﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Ports;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
//using System.Threading.Tasks;

namespace Module_Debug
{
    public partial class RM692D9_692 : Form
    {
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke  
        #region //==============CA310===============
        CaSDK Ca310Obj = new CaSDK();
        private double auto_lum;//
        private double auto_x;
        private double auto_y;
        private double auto_T;
        private double auto_duv;
        bool temptest255level = true;
        //创建类型为Person的对象集合
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();
        private static bool Gamma_adjust_timeout_flag = false;
        string[] bmpname = new string[15] {"A255.dsc_0001.bmp", "A240.dsc_0001.bmp", "A208.dsc_0001.bmp", "A144.dsc_0001.bmp", "A112.dsc_0001.bmp", "A80.dsc_0001.bmp", "A64.dsc_0001.bmp", "A40.dsc_0001.bmp",
                                           "A32.dsc_0001.bmp", "A24.dsc_0001.bmp", "A16.dsc_0001.bmp", "A8.dsc_0001.bmp", "A4.dsc_0001.bmp", "A1.dsc_0001.bmp", "A0.dsc_0001.bmp"};
        string[] bmpname31 = new string[31] {"A255.dsc_0001.bmp", "A254.dsc_0001.bmp", "A252.dsc_0001.bmp", "A248.dsc_0001.bmp", "A244.dsc_0001.bmp", "A236.dsc_0001.bmp", "A228.dsc_0001.bmp", "A220.dsc_0001.bmp",
                                           "A212.dsc_0001.bmp", "A204.dsc_0001.bmp", "A196.dsc_0001.bmp", "A188.dsc_0001.bmp", "A180.dsc_0001.bmp", "A164.dsc_0001.bmp", "A148.dsc_0001.bmp", "A132.dsc_0001.bmp", "A116.dsc_0001.bmp",
                                             "A100.dsc_0001.bmp", "A84.dsc_0001.bmp", "A68.dsc_0001.bmp", "A52.dsc_0001.bmp", "A44.dsc_0001.bmp", "A36.dsc_0001.bmp", "A28.dsc_0001.bmp", "A20.dsc_0001.bmp", "A16.dsc_0001.bmp",
                                            "A12.dsc_0001.bmp", "A8.dsc_0001.bmp", "A4.dsc_0001.bmp", "A3.dsc_0001.bmp", "A0.dsc_0001.bmp"};
        string[] aodbmpname = new string[15] {"EAOD_255.bmp", "EAOD_240.bmp", "EAOD_208.bmp", "EAOD_144.bmp", "EAOD_112.bmp", "EAOD_80.bmp", "EAOD_64.bmp", "EAOD_40.bmp",
                                           "EAOD_32.bmp", "EAOD_24.bmp", "EAOD_16.bmp", "EAOD_8.bmp", "EAOD_4.bmp", "EAOD_1.bmp", "EAOD_0.bmp"};
        //  string[] aodbmpname31 = new string[31] {"A255_0001.bmp", "A254.dsc_0001.bmp", "A252.dsc_0001.bmp", "A248.dsc_0001.bmp", "A244.dsc_0001.bmp", "A236.dsc_0001.bmp", "A228.dsc_0001.bmp", "A220.dsc_0001.bmp",
        //                                    "A212.dsc_0001.bmp", "A204.dsc_0001.bmp", "A196.dsc_0001.bmp", "A188.dsc_0001.bmp", "A180.dsc_0001.bmp", "A164.dsc_0001.bmp", "A148.dsc_0001.bmp", "A132.dsc_0001.bmp", "A116.dsc_0001.bmp",
        //                                      "A100.dsc_0001.bmp", "A84.dsc_0001.bmp", "A68.dsc_0001.bmp", "A52.dsc_0001.bmp", "A44.dsc_0001.bmp", "A36.dsc_0001.bmp", "A28.dsc_0001.bmp", "A20.dsc_0001.bmp", "A16.dsc_0001.bmp",
        //                                    "A12.dsc_0001.bmp", "A8.dsc_0001.bmp", "A4.dsc_0001.bmp", "A3.dsc_0001.bmp", "A0.dsc_0001.bmp"};
        //frmMain frmHelp = null;
        string strcurrentpath = System.Environment.CurrentDirectory;
        decimal deR1, deG1, deB1;
        CaSDK CaSDKObj = new CaSDK();
        private void Tim1Ca310_Tick(object sender, EventArgs e)
        {
            //textBox1.Text += "CA310 连接校准中...\r\n";
            //ca310info.Visible = true; //显示提示信息
            Tim1Ca310.Enabled = false;
        }
        private void btn_DisconnectCaX10_Click(object sender, EventArgs e)
        {
            //Ca310Obj.objCa.RemoteMode = 0;
        }
        #endregion

        #region //------CA410---------------
        CaSDK Ca41Obj = new CaSDK();
        public double Lv = 0.0;
        public double sx = 0.0;
        public double sy = 0.0;
        //string ca410Info = "";
        #endregion
        string vesa_bmp_path;

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过   
        private byte[] Rec_tmp = new byte[5];//
        private byte[] Rec_tmp1 = new byte[1];//
        private byte[] Send_buf = new byte[1];
        private byte signal_mode;
        public byte start_trans_flag = 0;
        public byte continueflag = 0;
        //public byte RM19X_0xFE_XX = 0;
        //public byte RM300_0XFE_XX = 0;
        public byte RM350_0XFE_XX = 0;
        //public byte RM010_0XFE_XX = 0;
        //public byte CH13721_0XFE_XX = 0;
        //public byte UD61720_0XFE_XX = 0;
        public byte Seil_port_Find_flag = 0;
        private byte[] Driver_IC = new byte[1];
        private byte[] OTP_Time_Data = new byte[15];
        private byte[] MTPDoneFlag = new byte[21];
        private List<string> ImageType;
        private delegate void MonitorSerialPortChangeEventHandler();
        private MonitorSerialPortChangeEventHandler MonitorCom;
        private int serialPortStatus;
        private int count = 0;
        //------------------------------------------

        //public static int RM692D9CmdType;
        public byte RM2AX_0XFE_XX = 0;

        CRC16 CRCData = new CRC16();

        #region//=================================Timer1=检测是否通讯故障=======================
        private void T1_Tick(object sender, EventArgs e)
        {
            T1.Enabled = false;
            //textBox1.Text = textBox1.Text + "通讯故障!\r\n";
            //textBox1.Focus();//获取焦点
            //textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
            //textBox1.ScrollToCaret();//滚动到光标处
        }
        //=================================Timer====end=========================
        #endregion
        #region  //串口数据处理
        private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            //if (MainForm.IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = MainForm.comPort.BytesToRead;
                byte[] buf = new byte[n];
                //byte data;

                MainForm.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    if (((buffer[0] == 0x2F) || (buffer[0] == 0x3F) || (buffer[0] == 0x9F) || (buffer[0] == 0x68)) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            //byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            //buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(buf, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, buffer.Count);    //正确分析一条数据，从缓存中移除数据。 
                            }
                        }
                    }
                    else
                        buffer.RemoveRange(0, buffer.Count);
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                     {
                         T1.Enabled = false; //停止超时计数

                         if (Rec_tmp[0] == 0x68)
                         {
                             switch (Rec_tmp[1])
                             {
                                 case 0x01: { if (Rec_tmp[4] == 0x00) textBox1.Text = textBox1.Text + "写寄存器\tOK！\r\n"; } break;
                                 case 0x02:
                                     {
                                         ReadedFlag = true;
                                         int i;
                                         string tmp = "";
                                         for (i = 3; i <= (Rec_tmp[2] + 1); i++)
                                         {
                                             tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]) + ",";//16进制
                                         }
                                         tmp = tmp + String.Format("{0:X2}", Rec_tmp[i]);

                                         //  RM692D9_Reg_Data.Text = tmp;
                                         textBox1.Text = textBox1.Text + "寄存器\t读 OK！\r\n";

                                     }
                                     break;
                                 case 0x04:
                                     {
                                         if (Rec_tmp[4] == 0x00)
                                         {
                                             if ((Rec_tmp[3] & 0xf0) == 0x00) textBox1.Text = textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                             else if ((Rec_tmp[3] & 0xf0) == 0x40) textBox1.Text = textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                             else if ((Rec_tmp[3] & 0xf0) == 0x80) textBox1.Text = textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                         }
                                     }
                                     break;
                                 case 0xF4:
                                     {
                                         if (Rec_tmp[4] == 0x00)
                                         {
                                             if ((Rec_tmp[3] & 0xf0) == 0x00) textBox1.Text = textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                             else if ((Rec_tmp[3] & 0xf0) == 0x40) textBox1.Text = textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                             else if ((Rec_tmp[3] & 0xf0) == 0x80) textBox1.Text = textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                         }
                                     }
                                     break;
                                 case 0x03:
                                     {
                                         if (Rec_tmp[4] == 0x00) textBox1.Text = textBox1.Text + "写Gamma1 OTP\t写 OK！\r\n";
                                         else if (Rec_tmp[4] == 0x02) textBox1.Text = textBox1.Text + "写Gamma1 OTP\t写 ERROR！\r\n";
                                     }
                                     break;
                                 case 0xDB:
                                     {
                                         if (Rec_tmp[3] == 0x01) textBox1.Text = textBox1.Text + "写GAM1  RAM OK！\r\n";
                                         else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写GAM5  RAM R！\r\n";
                                         else if (Rec_tmp[3] == 0x03) textBox1.Text = textBox1.Text + "写GAM9  RAM R！\r\n";
                                         else if (Rec_tmp[3] == 0x04) textBox1.Text = textBox1.Text + "写GAM20  RAM R！\r\n";
                                         else if (Rec_tmp[3] == 0x05) textBox1.Text = textBox1.Text + "写GAM21  RAM R！\r\n";
                                     }
                                     break;
                                 //case 0x10:
                                 //    {
                                 //        if (Rec_tmp[3] == 0x00) textBox1.Text = textBox1.Text + "写Power OTP\t写 OK！\r\n";
                                 //        else if (Rec_tmp[3] == 0x02) textBox1.Text = textBox1.Text + "写Power OTP\t写 ERROR！\r\n";
                                 //    }
                                 //    break;
                                 case 0x06:
                                     {
                                         string tmp = "";
                                         tmp = String.Format("{0:X2}", Rec_tmp[4]);
                                         //RM692D9_Time.Text = tmp;////
                                         textBox1.Text = textBox1.Text + "OTP\t读 OK！\r\n";
                                     }
                                     break;
                                 case 0x07:
                                     {
                                         textBox1.Text = textBox1.Text + "OTP OK！\r\n";
                                     }
                                     break;
                                 case 0x09:
                                     {
                                         if (Rec_tmp[4] == 0x00) textBox1.Text = textBox1.Text + "Change Gray\t写 OK！\r\n";
                                         else if (Rec_tmp[4] == 0x02) textBox1.Text = textBox1.Text + "Change GrayP\t写 ERROR！\r\n";
                                     }
                                     break;
                                 case 0x05:
                                     {
                                         if (Setting.TempRecord.RM692D9CmdType == 0x03) //red
                                         {
                                             {

                                                 RM692D9_0R1.Value = Decimal.Parse((Rec_tmp[6] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                 RM692D9_0R2.Value = Decimal.Parse((Rec_tmp[8] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                 RM692D9_0R3.Value = Decimal.Parse((Rec_tmp[10] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                 RM692D9_0R4.Value = Decimal.Parse((Rec_tmp[12] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                 RM692D9_0R5.Value = Decimal.Parse((Rec_tmp[14] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                 RM692D9_0R6.Value = Decimal.Parse((Rec_tmp[16] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                 RM692D9_0R7.Value = Decimal.Parse((Rec_tmp[18] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                 RM692D9_0R8.Value = Decimal.Parse((Rec_tmp[20] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                 RM692D9_0R9.Value = Decimal.Parse((Rec_tmp[22] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                 RM692D9_0R10.Value = Decimal.Parse((Rec_tmp[24] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                 RM692D9_0R11.Value = Decimal.Parse((Rec_tmp[26] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                 RM692D9_0R12.Value = Decimal.Parse((Rec_tmp[28] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                 RM692D9_0R13.Value = Decimal.Parse((Rec_tmp[30] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                 RM692D9_0R14.Value = Decimal.Parse((Rec_tmp[32] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                 RM692D9_0R15.Value = Decimal.Parse((Rec_tmp[34] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                             }
                                             textBox1.Text = textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                         }
                                         else if (Setting.TempRecord.RM692D9CmdType == 0x04) //green
                                         {
                                             {

                                                 RM692D9_0G1.Value = Decimal.Parse((Rec_tmp[6] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                 RM692D9_0G2.Value = Decimal.Parse((Rec_tmp[8] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                 RM692D9_0G3.Value = Decimal.Parse((Rec_tmp[10] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                 RM692D9_0G4.Value = Decimal.Parse((Rec_tmp[12] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                 RM692D9_0G5.Value = Decimal.Parse((Rec_tmp[14] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                 RM692D9_0G6.Value = Decimal.Parse((Rec_tmp[16] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                 RM692D9_0G7.Value = Decimal.Parse((Rec_tmp[18] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                 RM692D9_0G8.Value = Decimal.Parse((Rec_tmp[20] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                 RM692D9_0G9.Value = Decimal.Parse((Rec_tmp[22] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                 RM692D9_0G10.Value = Decimal.Parse((Rec_tmp[24] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                 RM692D9_0G11.Value = Decimal.Parse((Rec_tmp[26] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                 RM692D9_0G12.Value = Decimal.Parse((Rec_tmp[28] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                 RM692D9_0G13.Value = Decimal.Parse((Rec_tmp[30] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                 RM692D9_0G14.Value = Decimal.Parse((Rec_tmp[32] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                 RM692D9_0G15.Value = Decimal.Parse((Rec_tmp[34] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                             }
                                             textBox1.Text = textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                         }
                                         else if (Setting.TempRecord.RM692D9CmdType == 0x05) //blue
                                         {
                                             {

                                                 RM692D9_0B1.Value = Decimal.Parse((Rec_tmp[6] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                 RM692D9_0B2.Value = Decimal.Parse((Rec_tmp[8] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                 RM692D9_0B3.Value = Decimal.Parse((Rec_tmp[10] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                 RM692D9_0B4.Value = Decimal.Parse((Rec_tmp[12] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                 RM692D9_0B5.Value = Decimal.Parse((Rec_tmp[14] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                 RM692D9_0B6.Value = Decimal.Parse((Rec_tmp[16] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                 RM692D9_0B7.Value = Decimal.Parse((Rec_tmp[18] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                 RM692D9_0B8.Value = Decimal.Parse((Rec_tmp[20] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                 RM692D9_0B9.Value = Decimal.Parse((Rec_tmp[22] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                 RM692D9_0B10.Value = Decimal.Parse((Rec_tmp[24] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                 RM692D9_0B11.Value = Decimal.Parse((Rec_tmp[26] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                 RM692D9_0B12.Value = Decimal.Parse((Rec_tmp[28] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                 RM692D9_0B13.Value = Decimal.Parse((Rec_tmp[30] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                 RM692D9_0B14.Value = Decimal.Parse((Rec_tmp[32] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                 RM692D9_0B15.Value = Decimal.Parse((Rec_tmp[34] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                             }
                                             textBox1.Text = textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                         }

                                     }
                                     break;
                                 case 0xF5:
                                     {
                                         if (Setting.TempRecord.RM692D9CmdType == 0x03) //red
                                         {
                                             {

                                                 RM692D9_0R1.Value = Decimal.Parse((Rec_tmp[6] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                 RM692D9_0R2.Value = Decimal.Parse((Rec_tmp[8] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                 RM692D9_0R3.Value = Decimal.Parse((Rec_tmp[10] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                 RM692D9_0R4.Value = Decimal.Parse((Rec_tmp[12] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                 RM692D9_0R5.Value = Decimal.Parse((Rec_tmp[14] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                 RM692D9_0R6.Value = Decimal.Parse((Rec_tmp[16] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                 RM692D9_0R7.Value = Decimal.Parse((Rec_tmp[18] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                 RM692D9_0R8.Value = Decimal.Parse((Rec_tmp[20] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                 RM692D9_0R9.Value = Decimal.Parse((Rec_tmp[22] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                 RM692D9_0R10.Value = Decimal.Parse((Rec_tmp[24] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                 RM692D9_0R11.Value = Decimal.Parse((Rec_tmp[26] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                 RM692D9_0R12.Value = Decimal.Parse((Rec_tmp[28] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                 RM692D9_0R13.Value = Decimal.Parse((Rec_tmp[30] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                 RM692D9_0R14.Value = Decimal.Parse((Rec_tmp[32] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                 RM692D9_0R15.Value = Decimal.Parse((Rec_tmp[34] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                 RM692D9_0R16.Value = Decimal.Parse((Rec_tmp[36] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                 RM692D9_0R17.Value = Decimal.Parse((Rec_tmp[38] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                 RM692D9_0R18.Value = Decimal.Parse((Rec_tmp[40] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                 RM692D9_0R19.Value = Decimal.Parse((Rec_tmp[42] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                 RM692D9_0R20.Value = Decimal.Parse((Rec_tmp[44] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                 RM692D9_0R21.Value = Decimal.Parse((Rec_tmp[46] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                 RM692D9_0R22.Value = Decimal.Parse((Rec_tmp[48] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                 RM692D9_0R23.Value = Decimal.Parse((Rec_tmp[50] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                 RM692D9_0R24.Value = Decimal.Parse((Rec_tmp[52] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                 RM692D9_0R25.Value = Decimal.Parse((Rec_tmp[54] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                 RM692D9_0R26.Value = Decimal.Parse((Rec_tmp[56] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                                 RM692D9_0R27.Value = Decimal.Parse((Rec_tmp[58] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                                 RM692D9_0R28.Value = Decimal.Parse((Rec_tmp[60] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                                 RM692D9_0R29.Value = Decimal.Parse((Rec_tmp[62] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                                 RM692D9_0R30.Value = Decimal.Parse((Rec_tmp[64] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                                 RM692D9_0R31.Value = Decimal.Parse((Rec_tmp[66] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                             }
                                             textBox1.Text = textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                         }
                                         else if (Setting.TempRecord.RM692D9CmdType == 0x04) //green
                                         {
                                             {

                                                 RM692D9_0G1.Value = Decimal.Parse((Rec_tmp[6] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                 RM692D9_0G2.Value = Decimal.Parse((Rec_tmp[8] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                 RM692D9_0G3.Value = Decimal.Parse((Rec_tmp[10] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                 RM692D9_0G4.Value = Decimal.Parse((Rec_tmp[12] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                 RM692D9_0G5.Value = Decimal.Parse((Rec_tmp[14] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                 RM692D9_0G6.Value = Decimal.Parse((Rec_tmp[16] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                 RM692D9_0G7.Value = Decimal.Parse((Rec_tmp[18] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                 RM692D9_0G8.Value = Decimal.Parse((Rec_tmp[20] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                 RM692D9_0G9.Value = Decimal.Parse((Rec_tmp[22] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                 RM692D9_0G10.Value = Decimal.Parse((Rec_tmp[24] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                 RM692D9_0G11.Value = Decimal.Parse((Rec_tmp[26] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                 RM692D9_0G12.Value = Decimal.Parse((Rec_tmp[28] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                 RM692D9_0G13.Value = Decimal.Parse((Rec_tmp[30] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                 RM692D9_0G14.Value = Decimal.Parse((Rec_tmp[32] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                 RM692D9_0G15.Value = Decimal.Parse((Rec_tmp[34] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                 RM692D9_0G16.Value = Decimal.Parse((Rec_tmp[36] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                 RM692D9_0G17.Value = Decimal.Parse((Rec_tmp[38] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                 RM692D9_0G18.Value = Decimal.Parse((Rec_tmp[40] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                 RM692D9_0G19.Value = Decimal.Parse((Rec_tmp[42] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                 RM692D9_0G20.Value = Decimal.Parse((Rec_tmp[44] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                 RM692D9_0G21.Value = Decimal.Parse((Rec_tmp[46] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                 RM692D9_0G22.Value = Decimal.Parse((Rec_tmp[48] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                 RM692D9_0G23.Value = Decimal.Parse((Rec_tmp[50] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                 RM692D9_0G24.Value = Decimal.Parse((Rec_tmp[52] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                 RM692D9_0G25.Value = Decimal.Parse((Rec_tmp[54] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                 RM692D9_0G26.Value = Decimal.Parse((Rec_tmp[56] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                                 RM692D9_0G27.Value = Decimal.Parse((Rec_tmp[58] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                                 RM692D9_0G28.Value = Decimal.Parse((Rec_tmp[60] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                                 RM692D9_0G29.Value = Decimal.Parse((Rec_tmp[62] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                                 RM692D9_0G30.Value = Decimal.Parse((Rec_tmp[64] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                                 RM692D9_0G31.Value = Decimal.Parse((Rec_tmp[66] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                             }
                                             textBox1.Text = textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                         }
                                         else if (Setting.TempRecord.RM692D9CmdType == 0x05) //blue
                                         {
                                             {

                                                 RM692D9_0B1.Value = Decimal.Parse((Rec_tmp[6] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                                 RM692D9_0B2.Value = Decimal.Parse((Rec_tmp[8] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                                 RM692D9_0B3.Value = Decimal.Parse((Rec_tmp[10] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                                 RM692D9_0B4.Value = Decimal.Parse((Rec_tmp[12] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                                 RM692D9_0B5.Value = Decimal.Parse((Rec_tmp[14] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                                 RM692D9_0B6.Value = Decimal.Parse((Rec_tmp[16] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                                 RM692D9_0B7.Value = Decimal.Parse((Rec_tmp[18] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                                 RM692D9_0B8.Value = Decimal.Parse((Rec_tmp[20] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                                 RM692D9_0B9.Value = Decimal.Parse((Rec_tmp[22] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                                 RM692D9_0B10.Value = Decimal.Parse((Rec_tmp[24] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                                 RM692D9_0B11.Value = Decimal.Parse((Rec_tmp[26] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                                 RM692D9_0B12.Value = Decimal.Parse((Rec_tmp[28] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                                 RM692D9_0B13.Value = Decimal.Parse((Rec_tmp[30] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                                 RM692D9_0B14.Value = Decimal.Parse((Rec_tmp[32] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                                 RM692D9_0B15.Value = Decimal.Parse((Rec_tmp[34] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                                 RM692D9_0B16.Value = Decimal.Parse((Rec_tmp[36] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                                 RM692D9_0B17.Value = Decimal.Parse((Rec_tmp[38] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                                 RM692D9_0B18.Value = Decimal.Parse((Rec_tmp[40] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                                 RM692D9_0B19.Value = Decimal.Parse((Rec_tmp[42] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                                 RM692D9_0B20.Value = Decimal.Parse((Rec_tmp[44] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                                 RM692D9_0B21.Value = Decimal.Parse((Rec_tmp[46] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                                 RM692D9_0B22.Value = Decimal.Parse((Rec_tmp[48] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                                 RM692D9_0B23.Value = Decimal.Parse((Rec_tmp[50] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                                 RM692D9_0B24.Value = Decimal.Parse((Rec_tmp[52] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                                 RM692D9_0B25.Value = Decimal.Parse((Rec_tmp[54] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                                 RM692D9_0B26.Value = Decimal.Parse((Rec_tmp[56] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                                 RM692D9_0B27.Value = Decimal.Parse((Rec_tmp[58] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                                 RM692D9_0B28.Value = Decimal.Parse((Rec_tmp[60] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[61].ToString());
                                                 RM692D9_0B29.Value = Decimal.Parse((Rec_tmp[62] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[63].ToString());
                                                 RM692D9_0B30.Value = Decimal.Parse((Rec_tmp[64] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[65].ToString());
                                                 RM692D9_0B31.Value = Decimal.Parse((Rec_tmp[66] & 0x0F).ToString()) * 256 + Decimal.Parse(Rec_tmp[67].ToString());
                                             }
                                             textBox1.Text = textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                         }

                                     }
                                     break;
                                 case 0x0B:
                                     {
                                         for (int i = 0; i < 6; i++)
                                         {
                                             OTP_Time_Data[i] = Rec_tmp[4 + i];
                                         }
                                         textBox1.Text = textBox1.Text + "读寄存器\tOK！\r\n";
                                     }
                                     break;
                                 case 0x1B:
                                     {
                                         for (int i = 0; i < 21; i++)
                                         {
                                             MTPDoneFlag[i] = Rec_tmp[4 + i];
                                         }
                                         textBox1.Text = textBox1.Text + "MTP Done Falg Read Complete\r\n";
                                         MTPDoneFlagShow.Enabled = true;
                                     }
                                     break;
                                 case 0xAB:
                                     {
                                         textBox1.Text = textBox1.Text + "强切成功\r\n";
                                     }
                                     break;
                                 default: break;

                             }
                         }
                         ////---------------------------------------------------Demura----------------------------------------
                         else if ((Rec_tmp[0] == 0x2F))  //Demura//RM692D9
                         {
                             textBox1.Text = textBox1.Text + "RM692D9\r\n";
                             switch (Rec_tmp[1])
                             {
                                 case 0x01:
                                     {
                                         textBox1.Text = textBox1.Text + "Demura RAM Enable\r\n";
                                     }
                                     break;
                                 case 0x02:
                                     {
                                         textBox1.Text = textBox1.Text + "Demura RAM Disable\r\n";
                                     }
                                     break;
                                 case 0x03:
                                     {
                                         if (Rec_tmp[4] == 0x80)
                                         {
                                             textBox1.Text = textBox1.Text + "Demura RAM write Flash complete ! \r\n";
                                         }
                                     }
                                     break;
                                 case 0x04:
                                     {
                                         textBox1.Text = textBox1.Text + "Checksum: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                     }
                                     break;
                                 case 0x05:
                                     {
                                         if (Rec_tmp[4] == 0x80)
                                         {
                                             textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                         }
                                     }
                                     break;
                                 case 0x06:
                                     {
                                         textBox1.Text = textBox1.Text + "flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "\r\n";
                                     }
                                     break;
                                 case 0x07:
                                     {
                                         textBox1.Text = textBox1.Text + "Demura function on \r\n";
                                     }
                                     break;
                                 case 0x08:
                                     {
                                         textBox1.Text = textBox1.Text + "Demura function off \r\n";
                                     }
                                     break;
                                 case 0x09:
                                     {
                                         textBox1.Text = textBox1.Text + "Demura OTP complete$ \r\n";
                                     }
                                     break;
                                 default:
                                     break;
                             }
                         }
                         else if (Rec_tmp[0] == 0x9f)
                         {
                             textBox1.Text += "命令已执行" + "\r\n";
                         }
                         textBox1.Focus();//获取焦点
                         textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
                         textBox1.ScrollToCaret();//滚动到光标处
                         if (textBox1.Lines.Length > 200) textBox1.Text = "";
                     }));
                }
            }
            finally
            {
                Listening = false;
            }

        }
        #endregion

        #region//===========LCD cfg function:OLED_Show_Click====OLED显示=================
        private void Pattern_Write(byte R_value, byte G_value, byte B_value)
        {
            numPatternR.Value = R_value;
            numPatternG.Value = G_value;
            numPatternB.Value = B_value;
            OLED_Show_Click(btnPatternW, null);
        }
        private void OLED_Show_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            switch (btnTmp.Name)
            {

                case "btnPatternW":                             //写入Pattern参数
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                        Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                        Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                    }
                    break;
                case "btnPrevPic": //Send_buf[1] = 0x1B; break; //上一张图片
                    {
                        Send_buf[1] = 0x1b;
                    }
                    break;
                case "btnNextPic": //Send_buf[1] = 0x1C; break; //下一张图片
                    {
                        Send_buf[1] = 0x1c;
                    }
                    break;
                case "btnred":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //R
                case "btngreen":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0;
                    }
                    break; //G
                case "btnblue":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0xFF;
                    }
                    break; //B
                case "btnwhite":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0xFF;
                    }
                    break; //W
                case "btnblack":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //BL


                case "btnPattern16":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 16;
                        Send_buf[4] = 16;
                        Send_buf[5] = 16;
                    }
                    break; //16灰阶

                case "btnPattern32":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 32;
                        Send_buf[4] = 32;
                        Send_buf[5] = 32;
                    }
                    break; //32灰阶

                case "btnPattern64":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 64;
                        Send_buf[4] = 64;
                        Send_buf[5] = 64;
                    }
                    break; //64灰阶

                case "btnPattern96":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 96;
                        Send_buf[4] = 96;
                        Send_buf[5] = 96;
                    }
                    break; //96灰阶

                case "btnPattern128":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 128;
                        Send_buf[4] = 128;
                        Send_buf[5] = 128;
                    }
                    break; //128灰阶
                case "btnPattern192":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 192;
                        Send_buf[4] = 192;
                        Send_buf[5] = 192;
                    }
                    break; //192灰阶

                case "btnPattern224":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 224;
                        Send_buf[4] = 224;
                        Send_buf[5] = 224;
                    }
                    break; //224灰阶
                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 25);
                return;
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            Thread.Sleep(800);
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
                numPatternG.Enabled = false;
                numPatternB.Enabled = false;
            }
            else
            {
                numPatternG.Enabled = true;
                numPatternB.Enabled = true;
            }
        }
        private void numPatternR_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternR.Value;
            }
            OLED_Show_Click(btnPatternW, e);
            Thread.Sleep(2);
        }
        private void numPatternG_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternB.Value = numPatternG.Value;
                numPatternR.Value = numPatternG.Value;
            }
            OLED_Show_Click(btnPatternW, e);
            Thread.Sleep(2);
        }
        private void numPatternB_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternB.Value;
                numPatternR.Value = numPatternB.Value;
            }
            OLED_Show_Click(btnPatternW, e);
            Thread.Sleep(2);
        }
        #endregion
        //public class MyUpDown : NumericUpDown
        //{     //重写NumericUpDown
        //    public override void UpButton()
        //    {
        //        decimal inc = this.Increment;
        //        if ((Control.ModifierKeys & Keys.Control) == Keys.Control) this.Increment *= 10;
        //        base.UpButton();
        //        this.Increment = inc;
        //    }
        //    // TODO: DownButton
        //}

        #region       //--------------------Demura 操作---------------       

        [DllImport("user32.dll")]
        public static extern int MessageBoxTimeoutA(IntPtr hWnd, string msg, string Caps, int type, int Id, int time);//引用DLL

        UInt64 HexSize = 0;
        string Demurahexstring = "";
        private void Demura_Click(object sender, EventArgs e)
        {
            //string[] RM67298_Enable_Flash_Control = new string[4] { "fe", "20", "f3", "02"};
            Button btnTmp = (Button)sender;
            Send_buf = new byte[10];
            Setting.TempRecord.cmdLen = 10;
            Send_buf[0] = 0x2F;
            Send_buf[2] = 0x05;
            Send_buf[3] = 0x19;//
            Send_buf[7] = 0x3F;
            switch (btnTmp.Name)
            {
                case "Demura_Function_ON":
                    {
                        Send_buf[1] = 0x07;
                    }
                    break;
                case "Demura_Function_OFF":
                    {
                        Send_buf[1] = 0x08;
                    }
                    break;
                case "Inter_Demura_data":
                    {
                        //Send_buf[1] = 0x01;
                        string str = "";
                        StreamReader streamReader;
                        {
                            System.IO.FileInfo fileInfo = null;
                            string pathhex = "";
                            //pathhex= textBox5.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
                            try
                            {
                                //pathhex = textBox5.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
                                //fileInfo = new System.IO.FileInfo(pathhex);
                            }
                            catch
                            {
                                // 其他处理异常的代码
                                textBox1.Text = "未指定Hex文件！\r\n";
                                return;
                            }
                            // 如果文件存在
                            if (fileInfo != null && fileInfo.Exists)
                            {
                                string t1, t2;
                                System.Diagnostics.FileVersionInfo info = System.Diagnostics.FileVersionInfo.GetVersionInfo(pathhex);
                                Console.WriteLine("文件大小=" + System.Math.Ceiling(fileInfo.Length / 1.0) + "");
                                textBox1.Text += "文件大小=" + System.Math.Ceiling(fileInfo.Length / 1.0) + "";
                                HexSize = (UInt32)(System.Math.Ceiling(fileInfo.Length + 0.0));
                                //-----------------------
                                Demurahexstring = "FE,26" + "\r\n" + "04,CF" + "\r\n" + "10,4B" + "\r\n" + "14,22" + "\r\n" + "1E,21" + "\r\n" + "FE,20" + "\r\n" + "5F,01" + "\r\n" + "09,13" + "\r\n"
                                                     + "FE,26" + "\r\n" + "F3,02" + "\r\n"
                                                     + "18," + ((HexSize >> 16).ToString("X2")) + "\r\n" + "19," + (((HexSize >> 8) & 0xff).ToString("X2")) + "\r\n" + "1A," + (((HexSize) & 0xff).ToString("X2")) + "\r\n"
                                                     + "FE,A0\r\n 0B,11\r\n fe,d0\r\n 58,53\r\n 07,8d\r\n 42,81\r\n 55,58\r\n fe,d2\r\n 6e,20\r\n fe,40\r\n 5b,01\r\n 5e,40\r\n 0e,40\r\n" + "FE,00" + "\r\n";
                                MainForm.txtProcessDrivercode(Demurahexstring, out t1, out t2);
                                Setting.TempRecord.cmdLen = 6;
                                MainForm.SendCmd(0);
                                textBox1.Text += "EN命令下发...\r\n";
                                textBox1.Select(textBox1.Text.Length, 0);
                                textBox1.ScrollToCaret();
                            }
                            else
                            {
                                Console.WriteLine("指定的文件路径不正确!");
                            }
                            //textBox1.Text += "未选择产品型号！！！\r\n";
                            return;
                        }
                    }

                case "Exit_Demura_data":
                    {
                        Send_buf[1] = 0x02;
                    }
                    break;
                case "Flash_Write_Demura_data":
                    {
                        textBox1.Text = "Flash 烧录中，请等待12S...";

                        string str = "";
                        StreamReader streamReader = new StreamReader("./x_Raydium/DemuraConfig/write_Flash.gcfg", Encoding.Default);
                        str = streamReader.ReadToEnd();
                        MainForm.txtProcessDrivercode(str, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        MainForm.SendCmd(0); //(0):低速； （4）高速
                        return;
                    }
                case "btnIintFlash":
                    {
                        //Send_buf[1] = 0x01;
                        string str = "";
                        {
                            StreamReader streamReader = new StreamReader("./x_Raydium/DemuraConfig/InitFlash_692dx.gcfg", Encoding.Default);
                            str = streamReader.ReadToEnd();
                        }
                        MainForm.txtProcessDrivercode(str, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        MainForm.SendCmd(0); //(0):低速； （4）高速
                        return;
                    }

                case "Flash_Check_Demura_data":
                    {
                        Send_buf[1] = 0x04;
                    }
                    break;
                case "Flash_Erase_Demura_data":
                    {
                        Send_buf[1] = 0x05;
                    }
                    break;
                case "Demura_Flash_Enable":
                    {
                        Send_buf[1] = 0x06;
                    }
                    break;
                case "Flash_Demura_OTP":
                    {
                        textBox1.Text += "请.手动.加载.烧录Demura算法！\r\n";
                        { Send_buf[1] = 0x09; }
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[8], out Send_buf[9]);
            if (Send_buf[1] == 0x03) //烧录前预加载Demura文件
            {
                ;//PreLoad_Demura_data_Click();
            }
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 10);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
            if (Send_buf[1] == 0x03)//烧写时给出相应耗时提示
            {
                MessageBoxTimeoutA((IntPtr)0, "烧写预计需要耗时15S，请留意信息窗烧录完成提示...", "---Info---15S后本窗口自动关闭---", 0, 0, 19000);
            }
            else if (Send_buf[1] == 0x05)  //添加擦除等待提示
            {
                MessageBoxTimeoutA((IntPtr)0, "擦除中，大约需要15S，请稍后...", "---Info---15S后本窗口自动关闭---", 0, 0, 19000);// 直接调用  3秒后自动关闭 父窗口句柄没有直接用0代替
                //原型：
                //int MessageBoxTimeoutA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType, WORD wLanguageId, DWORD dwMilliseconds);
                //int MessageBoxTimeoutW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType, WORD wLanguageId, DWORD dwMilliseconds)[1]
                //hwnd//消息框的拥有窗口。
                //lptext//消息框的内容。
                //lpCaption//消息框的标题。
                //uType//指定一个决定对话框的内容和行为的位标志集。取值参照函数MessageBox的参数uType。
                //wLanguageId//函数扩展，一般取0。
                //dwMilliseconds//消息框延迟关闭时间，单位：毫秒。
                //返回值
                //参照函数MessageBox的返回值。
                //如果超时，即用户未操作，消息框自动关闭，返回32000。
            }


        }

        private void btnReInit_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.btnReInit, "重新初始化LCD,但此按键偶尔会失效");
        }
        #endregion---------

        //private int adjust_num = 0;
        //private string adjust_class = null;

        //private double lum_off, x_off, y_off;
        private int index = 0;
        //private int flag1 = 1;
        //private int flag2 = 1;
        //private int flag3 = 1;
        private int adjust_Rnum = 0;
        private int adjust_Gnum = 0;
        private int adjust_Bnum = 0;
        private int adjust_l = 0;
        private int adjust_x = 0;
        private int adjust_y = 0;
        //private int step_l = 1;
        //private int step_m = 5;
        //private int step_h = 12;
        Gamma_Addr GAMMAs = new Gamma_Addr();
        private ushort[] gamma_r = new ushort[15];
        private ushort[] gamma_g = new ushort[15];
        private ushort[] gamma_b = new ushort[15];
        private double lum_off, x_off, y_off;
        private double R_x = 0.6756;//
        private double R_y = 0.3222;
        private double G_x = 0.2129;
        private double G_y = 0.7378;
        private double B_x = 0.1412;
        private double B_y = 0.0356;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        #region //-----------RM692D9_Start----------------------------------------
        #region //---Gamma调整---
        private double[,] D_lums;   //目标值-亮度拆分成RGB
        private double[] D_lum;     //目标值-白色
        private double D_x, D_y;    //记录预设色坐标
        private bool[] PointCheck = new bool[16];//记录绑点是否选定
        private static int[,] RGBValue = new int[18, 3];//记录绑点RGB寄存器值
        private int step_l = 1;
        private int step_m = 5;
        private int step_h = 12;
        private int step_hh = 16;
        bool array2value = false;   //记录值写入窗体控件(记录绑点RGB寄存器值)的标志位
        bool value2array = true;    //窗体控件值保存到记录标志位
        private void RM692D9_Gamma_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            if (radioButtonbd15.Checked)
            {
                Send_buf = new byte[38];
                Setting.TempRecord.cmdLen = 38;
                Send_buf[0] = 0x68; //RM692D9
                Send_buf[2] = 33;
                Send_buf[3] = 0;

                switch (btnTmp.Name)
                {
                    case "buttonR_Red":
                    case "RM692D9_GammaR_Red":
                        {
                            Setting.TempRecord.RM692D9CmdType = 0x03;       //      3
                            Send_buf[1] = 0x05;                             //读 Red Gamma
                            Send_buf[3] |= 0x00;
                            if (Page50.Checked == true) { Send_buf[4] = 0x01; }
                            else if (Page58.Checked == true) { Send_buf[4] = 0x05; }
                            else if (Page52H.Checked == true) { Send_buf[4] = 0x03; }

                        }
                        break;
                    case "buttonR_Green":
                    case "RM692D9_GammaR_Green":
                        {
                            Setting.TempRecord.RM692D9CmdType = 0x04;   //      4
                            Send_buf[1] = 0x05;                         //  读Gamma
                            Send_buf[3] |= 0x42;                        //绿色 green
                            if (Page50.Checked == true) { Send_buf[4] = 0x01; }
                            else if (Page58.Checked == true) { Send_buf[4] = 0x05; }
                            else if (Page52H.Checked == true) { Send_buf[4] = 0x03; }
                        }
                        break;
                    case "buttonR_Blue":
                    case "RM692D9_GammaR_Blue": //              9
                        {
                            Setting.TempRecord.RM692D9CmdType = 0x05;   //      5
                            Send_buf[1] = 0x05;                         //   读Gamma
                            Send_buf[3] |= 0x81;                        //   blue,此处的0x81下位机未使用
                            if (Page50.Checked == true) { Send_buf[4] = 0x01; }
                            else if (Page58.Checked == true) { Send_buf[4] = 0x05; }
                            else if (Page52H.Checked == true) { Send_buf[4] = 0x03; }
                        }
                        break;
                    //--------------------------------------
                    case "RM692D9_GammaW_Red":
                        {
                            ;
                        }
                        break;
                    case "RM692D9_GammaW_Green":
                        {
                            ;
                        }
                        break;
                    case "RM692D9_GammaW_Blue":     //      8
                        {
                            ;
                        }
                        break;
                    //------------------------------------------------
                    case "buttonW_Red":
                        {
                            Send_buf[1] = 0x04; //写Gamma
                            if (Page50.Checked == true) { Send_buf[3] = 0x00; }
                            else if (Page58.Checked == true) { Send_buf[3] = 0x04; }
                            else if (Page52H.Checked == true) { Send_buf[3] = 0x03; }
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R2.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R2.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R3.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R3.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R4.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R4.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R5.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R5.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R6.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R6.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R7.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R7.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R8.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R8.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R9.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R9.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R10.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R10.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R11.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R11.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R12.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R12.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R13.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R13.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R14.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R14.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R15.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R15.Value.ToString())) & 0xff);
                        }
                        break;
                    case "buttonW_Green":
                        {
                            Send_buf[1] = 0x04;
                            if (Page50.Checked == true) { Send_buf[3] = 0x40; }
                            else if (Page58.Checked == true) { Send_buf[3] = 0x44; }
                            else if (Page52H.Checked == true) { Send_buf[3] = 0x43; }
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G2.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G2.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G3.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G3.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G4.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G4.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G5.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G5.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G6.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G6.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G7.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G7.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G8.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G8.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G9.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G9.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G10.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G10.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G11.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G11.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G12.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G12.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G13.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G13.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G14.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G14.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G15.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G15.Value.ToString())) & 0xff);
                        }
                        break;
                    case "buttonW_Blue":     //      8
                        {
                            Send_buf[1] = 0x04;
                            if (Page50.Checked == true) { Send_buf[3] = 0x80; }
                            else if (Page58.Checked == true) { Send_buf[3] = 0x84; }
                            else if (Page52H.Checked == true) { Send_buf[3] = 0x83; }
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B2.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B2.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B3.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B3.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B4.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B4.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B5.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B5.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B6.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B6.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B7.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B7.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B8.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B8.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B9.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B9.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B10.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B10.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B11.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B11.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B12.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B12.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B13.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B13.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B14.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B14.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B15.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B15.Value.ToString())) & 0xff);

                        }
                        break;
                    default:
                        break;
                }
                CRCData.CalculateCrc16(Send_buf, out Send_buf[36], out Send_buf[37]);
                try
                {
                    MainForm.comPort.Write(Send_buf, 0, 38);   //RM69350
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                }

            }
            else if (radioButtonbd31.Checked)
            {
                Send_buf = new byte[70];
                Setting.TempRecord.cmdLen = 70;
                Send_buf[0] = 0x68; //RM692D9
                Send_buf[2] = 65;
                Send_buf[3] = 0;

                switch (btnTmp.Name)
                {
                    case "buttonR_Red":
                    case "RM692D9_GammaR_Red":
                        {
                            Setting.TempRecord.RM692D9CmdType = 0x03;       //      3
                            Send_buf[1] = 0xF5;                             //读 Red Gamma
                            Send_buf[3] |= 0x00;
                            if (Page50.Checked == true) { Send_buf[4] = 0x01; }
                            else if (Page58.Checked == true) { Send_buf[4] = 0x05; }
                        }
                        break;
                    case "buttonR_Green":
                    case "RM692D9_GammaR_Green":
                        {
                            Setting.TempRecord.RM692D9CmdType = 0x04;   //      4
                            Send_buf[1] = 0xF5;                         //  读Gamma
                            Send_buf[3] |= 0x42;                        //绿色 green
                            if (Page50.Checked == true) { Send_buf[4] = 0x01; }
                            else if (Page58.Checked == true) { Send_buf[4] = 0x05; }
                        }
                        break;
                    case "buttonR_Blue":
                    case "RM692D9_GammaR_Blue": //              9
                        {
                            Setting.TempRecord.RM692D9CmdType = 0x05;   //      5
                            Send_buf[1] = 0xF5;                         //   读Gamma
                            Send_buf[3] |= 0x81;                        //   blue,此处的0x81下位机未使用
                            if (Page50.Checked == true) { Send_buf[4] = 0x01; }
                            else if (Page58.Checked == true) { Send_buf[4] = 0x05; }
                        }
                        break;
                    //--------------------------------------
                    case "RM692D9_GammaW_Red":
                        {
                            ;
                        }
                        break;
                    case "RM692D9_GammaW_Green":
                        {
                            ;
                        }
                        break;
                    case "RM692D9_GammaW_Blue":     //      8
                        {
                            Send_buf[1] = 0xF4;
                            if (Page50.Checked == true) { Send_buf[3] = 0x80; }
                            else if (Page58.Checked == true) { Send_buf[3] = 0x84; }
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B2.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B2.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B3.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B3.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B4.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B4.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B5.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B5.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B6.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B6.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B7.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B7.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B8.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B8.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B9.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B9.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B10.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B10.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B11.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B11.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B12.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B12.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B13.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B13.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B14.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B14.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B15.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B15.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B16.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B16.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B17.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B17.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B18.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B18.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B19.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B19.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B20.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B20.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B21.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B21.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B22.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B22.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B23.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B23.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B24.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B24.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B25.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B25.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B26.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B26.Value.ToString())) & 0xff);
                            Send_buf[58] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B27.Value.ToString()) >> 8);
                            Send_buf[59] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B27.Value.ToString())) & 0xff);
                            Send_buf[60] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B28.Value.ToString()) >> 8);
                            Send_buf[61] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B28.Value.ToString())) & 0xff);
                            Send_buf[62] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B29.Value.ToString()) >> 8);
                            Send_buf[63] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B29.Value.ToString())) & 0xff);
                            Send_buf[64] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B30.Value.ToString()) >> 8);
                            Send_buf[65] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B30.Value.ToString())) & 0xff);
                            Send_buf[66] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B31.Value.ToString()) >> 8);
                            Send_buf[67] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B31.Value.ToString())) & 0xff);

                        }
                        break;
                    //------------------------------------------------
                    case "buttonW_Red":
                        {
                            Send_buf[1] = 0xF4; //写Gamma
                            if (Page50.Checked == true) { Send_buf[3] = 0x00; }
                            else if (Page58.Checked == true) { Send_buf[3] = 0x04; }
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R2.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R2.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R3.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R3.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R4.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R4.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R5.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R5.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R6.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R6.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R7.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R7.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R8.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R8.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R9.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R9.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R10.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R10.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R11.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R11.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R12.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R12.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R13.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R13.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R14.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R14.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R15.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R15.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R16.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R16.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R17.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R17.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R18.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R18.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R19.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R19.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R20.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R20.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R21.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R21.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R22.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R22.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R23.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R23.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R24.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R24.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R25.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R25.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R26.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R26.Value.ToString())) & 0xff);
                            Send_buf[58] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R27.Value.ToString()) >> 8);
                            Send_buf[59] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R27.Value.ToString())) & 0xff);
                            Send_buf[60] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R28.Value.ToString()) >> 8);
                            Send_buf[61] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R28.Value.ToString())) & 0xff);
                            Send_buf[62] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R29.Value.ToString()) >> 8);
                            Send_buf[63] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R29.Value.ToString())) & 0xff);
                            Send_buf[64] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R30.Value.ToString()) >> 8);
                            Send_buf[65] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R30.Value.ToString())) & 0xff);
                            Send_buf[66] = Convert.ToByte(Convert.ToUInt16(RM692D9_0R31.Value.ToString()) >> 8);
                            Send_buf[67] = Convert.ToByte((Convert.ToUInt16(RM692D9_0R31.Value.ToString())) & 0xff);
                        }
                        break;
                    case "buttonW_Green":
                        {
                            Send_buf[1] = 0xF4;
                            if (Page50.Checked == true) { Send_buf[3] = 0x40; }
                            else if (Page58.Checked == true) { Send_buf[3] = 0x44; }
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G2.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G2.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G3.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G3.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G4.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G4.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G5.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G5.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G6.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G6.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G7.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G7.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G8.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G8.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G9.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G9.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G10.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G10.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G11.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G11.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G12.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G12.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G13.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G13.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G14.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G14.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G15.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G15.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G16.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G16.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G17.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G17.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G18.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G18.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G19.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G19.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G20.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G20.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G21.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G21.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G22.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G22.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G23.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G23.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G24.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G24.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G25.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G25.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G26.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G26.Value.ToString())) & 0xff);
                            Send_buf[58] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G27.Value.ToString()) >> 8);
                            Send_buf[59] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G27.Value.ToString())) & 0xff);
                            Send_buf[60] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G28.Value.ToString()) >> 8);
                            Send_buf[61] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G28.Value.ToString())) & 0xff);
                            Send_buf[62] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G29.Value.ToString()) >> 8);
                            Send_buf[63] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G29.Value.ToString())) & 0xff);
                            Send_buf[64] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G30.Value.ToString()) >> 8);
                            Send_buf[65] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G30.Value.ToString())) & 0xff);
                            Send_buf[66] = Convert.ToByte(Convert.ToUInt16(RM692D9_0G31.Value.ToString()) >> 8);
                            Send_buf[67] = Convert.ToByte((Convert.ToUInt16(RM692D9_0G31.Value.ToString())) & 0xff);
                        }
                        break;
                    case "buttonW_Blue":     //      8
                        {
                            Send_buf[1] = 0xF4;
                            if (Page50.Checked == true) { Send_buf[3] = 0x80; }
                            else if (Page58.Checked == true) { Send_buf[3] = 0x84; }
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B2.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B2.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B3.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B3.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B4.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B4.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B5.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B5.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B6.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B6.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B7.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B7.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B8.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B8.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B9.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B9.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B10.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B10.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B11.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B11.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B12.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B12.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B13.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B13.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B14.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B14.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B15.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B15.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B16.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B16.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B17.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B17.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B18.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B18.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B19.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B19.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B20.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B20.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B21.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B21.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B22.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B22.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B23.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B23.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B24.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B24.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B25.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B25.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B26.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B26.Value.ToString())) & 0xff);
                            Send_buf[58] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B27.Value.ToString()) >> 8);
                            Send_buf[59] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B27.Value.ToString())) & 0xff);
                            Send_buf[60] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B28.Value.ToString()) >> 8);
                            Send_buf[61] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B28.Value.ToString())) & 0xff);
                            Send_buf[62] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B29.Value.ToString()) >> 8);
                            Send_buf[63] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B29.Value.ToString())) & 0xff);
                            Send_buf[64] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B30.Value.ToString()) >> 8);
                            Send_buf[65] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B30.Value.ToString())) & 0xff);
                            Send_buf[66] = Convert.ToByte(Convert.ToUInt16(RM692D9_0B31.Value.ToString()) >> 8);
                            Send_buf[67] = Convert.ToByte((Convert.ToUInt16(RM692D9_0B31.Value.ToString())) & 0xff);

                        }
                        break;
                    default:
                        break;
                }
                CRCData.CalculateCrc16(Send_buf, out Send_buf[68], out Send_buf[69]);
                try
                {
                    MainForm.comPort.Write(Send_buf, 0, 70);   //RM69350
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                }

            }

            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }

        private int[] Point_692AX = new int[15] {//---Gamma绑点数据，这里的数据仅供参考---
            255, 240, 208, 144, 112, 80, 64, 40, 32, 24, 16, 8, 4, 1, 0};


        #region  //Gamma调整计时
        System.DateTime TimeNow = new DateTime();
        TimeSpan TimeCount = new TimeSpan();
        #endregion

        private static int GammaAdjustTimes = 0;
        //--------------------------------------------------------------------
        private int teppoint = 1; //记录绑点是否变更        
        private int frgb = 1;//当前调节RGB标志， 1:r;2:G;3:B;
        //private int numgammastep=numGammstep.Value;
        int step_tmpr = 64, step_tmpg = 64, step_tmpb = 64;//调节初始步长
        bool fdirr = true;
        bool fdirg = true;
        bool fdirb = true;
        byte nowdirection = 0, lastdirection = 0;
        byte nowdirectionG = 0, lastdirectionG = 0; byte nowdirectionB = 0, lastdirectionB = 0;
        byte GammaAdjN_COUNT = 0; byte GammaAdjN_COUNTG = 0; byte GammaAdjN_COUNTB = 0;
        //------------------------------------------------------------
        byte k = 0;
        byte extraInfo;
        private void btn2AXStop_Click(object sender, EventArgs e)   //--停止Gamma调整--
        {
            TimTest.Stop();//停止计时
            T18.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;

            R_x = 0.6756;//
            R_y = 0.3222;
            G_x = 0.2129;
            G_y = 0.7378;
            B_x = 0.1412;
            B_y = 0.0356;
        }
        bool lumdataReadyFlag;
        #endregion
        #region //--------------------------亮度量测----
        private int CAS200_Measure_Gray = 255;
        private int CAS200_Measure_Count = 0;
        private double[] lumRead = new double[4096]; //暂存亮度量测数据
        #endregion
        private void RM692D9_51Set(UInt16 valu51)   //---//直接通过下位机写入51寄存器
        {
            Send_buf = new byte[8];
            Setting.TempRecord.cmdLen = 8;
            valu51 &= 0x0FFF;
            Send_buf[0] = 0x3F;        //RM692D9
            Send_buf[1] = 0x51;
            Send_buf[2] = 0x03;
            //for (i = 0; i < 3; i++)
            //{
            //    Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
            //}
            Send_buf[3] = 0x51;
            Send_buf[4] = (Byte)(valu51 >> 8);
            Send_buf[5] = (Byte)valu51;

            CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 8);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }

        private void BtnMTPRead_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[27];
            Setting.TempRecord.cmdLen = 27;

            Send_buf[0] = 0x3F;        //RM692D9
            Send_buf[1] = 0x1B;   //Read All MTP Done Flag
            Send_buf[2] = 27 - 5;
            CRCData.CalculateCrc16(Send_buf, out Send_buf[25], out Send_buf[26]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 27);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        //------------Done Flag Reg =DFR------
        private readonly byte DFRAE = 2; private readonly byte DFRAF = 3; private readonly byte DFRB0 = 4; private readonly byte DFRB1 = 5;
        private readonly byte DFRB4 = 6; private readonly byte DFRB3 = 7; private readonly byte DFRB2 = 8; private readonly byte DFRA9 = 9;
        private readonly byte DFRAA = 10; private readonly byte DFRAB = 11; private readonly byte DFRBA = 12; private readonly byte DFRBB = 13;
        private readonly byte DFRAD = 14; private readonly byte DFRAC = 15; private readonly byte DFRB7 = 16; private readonly byte DFRB6 = 17;
        private readonly byte DFRB5 = 18; private readonly byte DFRB9 = 19; private readonly byte DFRB8 = 20;
        public void RM692D9_OTP_Read(object sender, EventArgs e)     //读取所有PAGE页的OTP次数       
        {
            Send_buf = new byte[17];

            Setting.TempRecord.cmdLen = 17;

            Send_buf[0] = 0x3F;        //RM692D9
            Send_buf[1] = 0x0B;
            Send_buf[2] = 12;
            CRCData.CalculateCrc16(Send_buf, out Send_buf[15], out Send_buf[16]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 17);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
            Application.DoEvents();
        }
        private void Creat_BMP(string[] text)
        {
            #endregion
            ///生成BMP
            ///
            Bitmap bmp2 = new Bitmap(1080, 2340, PixelFormat.Format24bppRgb);                      //设置分辨率及24位深BMP
            for (int i = 0; i < bmp2.Width; i++)
            {
                int gary = 0;
                for (int j = 0; j < bmp2.Height; j++)
                {
                    if (((j % 9) == 0) && (gary < 255))
                    {
                        gary++;
                    }
                    Color c = Color.FromArgb(0 + gary, 0 + gary, 0 + gary);                       //生成灰阶过度图H方向
                    bmp2.SetPixel(i, j, c);
                }
            }
            Graphics g = Graphics.FromImage(bmp2);

            ///打印文字
            ///
            //string text = "1234567890";
            float fontSize = 38f;                       //字体大小设置
            //float textWidth = text.Length * fontSize;
            System.Drawing.Font font = new System.Drawing.Font("加粗", fontSize, FontStyle.Bold);//字型设置

            for (int i = 0; i < text.Length; i++)
            {
                float rectX = bmp2.Width / 10;                                                   //文本框宽度
                float rectY = bmp2.Height / 20 + fontSize * i * 2;                                 //文本框高度
                float rectWidth = (2 * fontSize + font.Height * 2) * 2;                            //文本框在图片上X坐标
                float rectHeight = (fontSize / 72) * 96;                                         //文本框在图片上Y坐标
                RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);

                Brush whiteBrush = new SolidBrush(Color.WhiteSmoke);                            //字体颜色

                Brush blackBrush = new SolidBrush(Color.SlateGray);                            //文本框颜色

                g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);

                g.DrawString(text[i], font, whiteBrush, textArea);
            }

            bmp2.Save(System.Windows.Forms.Application.StartupPath + "\\Resources\\OTP_Time.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            //bmp2.Save(@"C:\\Users\\hanchong\\Desktop\\新建文件夹\\33.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            bmp2.Dispose();
            RM692D9_btnPrevPic_Run("OTP_Time.bmp");//发送图片
        }
        private void RM692D9_btnPrevPic_Run(string filenames)
        {
            Int32 HACT_True = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            byte[] TemFileContent = new byte[1024 * 100];
            System.Int32 ByteReaded = 0x00;
            try
            {
                string FileName;
                //string FileName = textBox2.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString();  vesa_bmp_path
                // if (Vesa_Check.Checked == true)
                {
                    FileName = vesa_bmp_path + '\\' + filenames;
                }
                //  else
                {
                    FileName = System.Windows.Forms.Application.StartupPath + "\\Resources\\" + filenames;
                }
                //string FileName = vesa_bmp_path + '\\' + filenames;
                FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                ByteReaded = aFile.Read(TemFileContent, 0, 54);
                HACT_True = (Convert.ToInt32(TemFileContent[19]) << 8) + TemFileContent[18];
                VACT_True = Convert.ToInt32((TemFileContent[23] << 8) + TemFileContent[22]);
                j = 1;
                //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                {
                    aFile.Seek(54, SeekOrigin.Begin);
                    while (j <= VACT_True)
                    {
                        if (HACT_True % 4 != 0)
                            aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                        else
                            aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                        ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                        try
                        {
                            MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            break;
                        }
                        m_txlen += ByteReaded;
                        //progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        //label14.Text = progressBar1.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        j++;
                    }

                    TemFileContent[0] = 0x5A;
                    try
                    {
                        MainForm.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }

                    aFile.Close();
                }
                /*
                else
                {
                    MessageBox.Show("BMP分辨率非法");
                }*/
            }
            catch
            {
                MessageBox.Show("请选择文件夹", "错误");
            }
        }
        string pcstepstr = "";
        private void btnReadReg_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[20];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 20;

            string strLine = "fe,40"; //9a
            string[] arr = strLine.Split(','); //以逗号分割，添加到数组

            Setting.TempRecord.cmdLen = arr.Length + 5;
            Send_buf = new byte[arr.Length + 5];
            Send_buf[0] = 0x3F;        //RM692D9
            Send_buf[1] = 0x01; //寄存器写
            Send_buf[2] = byte.Parse(arr.Length.ToString());
            for (int i = 0; i < arr.Length; i++)
            {
                Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
            Thread.Sleep(1000);

            strLine = "9a";//Convert.ToString(RM692D9_Reg_Data.Text);

            arr = strLine.Split(','); //以逗号分割，添加到数组
            int DataNum = 2;             //Convert.ToByte(tb_RM692D9_return_num.Text);
            Setting.TempRecord.cmdLen = DataNum + 1 + 5;
            Send_buf = new byte[DataNum + 1 + 5];
            Send_buf[0] = 0x3F;        //RM692D9
            Send_buf[1] = 0x02; //read function
            Send_buf[2] = Convert.ToByte(DataNum + 1);//length
            Send_buf[3] = byte.Parse((Convert.ToInt32(arr[0], 16)).ToString());//register               
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        //--------------------------------2018.08.09-----寄存器回读----------
        bool ReadedFlag = false;
        byte[] ReadBuf = new byte[2];
        //RM692D9_Others_Click
        //--------------------------------------------------------------------

        #region//--------------窗口处理-----------------
        frmMain MainForm = null;

        public RM692D9_692(frmMain Frm)
        {
            MainForm = Frm;
            InitializeComponent();
            //MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
        }
        private void btnBack_Click(object sender, EventArgs e)
        {   //返回主窗口
            MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);//移除本窗口串口处理函数
            MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);//
            MainForm.Show();
            this.Hide();
        }

        private void btnCa410Connect_Click(object sender, EventArgs e)
        {

        }

        private void LoadGamma_Click(object sender, EventArgs e)
        {

            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();

                string[] arrtmp = strtmp.Split(new Char[] { ',' });

                //if (MainForm.IC_comboBox.SelectedIndex == 20-1) //RM692D9  DJ_ADD
                if (radioButtonbd15.Checked)
                {
                    RM692D9_0R1.Text = arrtmp[1].ToString(); RM692D9_0G1.Text = arrtmp[3].ToString(); RM692D9_0B1.Text = arrtmp[5].ToString();
                    RM692D9_0R2.Text = arrtmp[7].ToString(); RM692D9_0G2.Text = arrtmp[9].ToString(); RM692D9_0B2.Text = arrtmp[11].ToString();
                    RM692D9_0R3.Text = arrtmp[13].ToString(); RM692D9_0G3.Text = arrtmp[15].ToString(); RM692D9_0B3.Text = arrtmp[17].ToString();
                    RM692D9_0R4.Text = arrtmp[19].ToString(); RM692D9_0G4.Text = arrtmp[21].ToString(); RM692D9_0B4.Text = arrtmp[23].ToString();
                    RM692D9_0R5.Text = arrtmp[25].ToString(); RM692D9_0G5.Text = arrtmp[27].ToString(); RM692D9_0B5.Text = arrtmp[29].ToString();
                    RM692D9_0R6.Text = arrtmp[31].ToString(); RM692D9_0G6.Text = arrtmp[33].ToString(); RM692D9_0B6.Text = arrtmp[35].ToString();
                    RM692D9_0R7.Text = arrtmp[37].ToString(); RM692D9_0G7.Text = arrtmp[39].ToString(); RM692D9_0B7.Text = arrtmp[41].ToString();
                    RM692D9_0R8.Text = arrtmp[43].ToString(); RM692D9_0G8.Text = arrtmp[45].ToString(); RM692D9_0B8.Text = arrtmp[47].ToString();
                    RM692D9_0R9.Text = arrtmp[49].ToString(); RM692D9_0G9.Text = arrtmp[51].ToString(); RM692D9_0B9.Text = arrtmp[53].ToString();
                    RM692D9_0R10.Text = arrtmp[55].ToString(); RM692D9_0G10.Text = arrtmp[57].ToString(); RM692D9_0B10.Text = arrtmp[59].ToString();
                    RM692D9_0R11.Text = arrtmp[61].ToString(); RM692D9_0G11.Text = arrtmp[63].ToString(); RM692D9_0B11.Text = arrtmp[65].ToString();
                    RM692D9_0R12.Text = arrtmp[67].ToString(); RM692D9_0G12.Text = arrtmp[69].ToString(); RM692D9_0B12.Text = arrtmp[71].ToString();
                    RM692D9_0R13.Text = arrtmp[73].ToString(); RM692D9_0G13.Text = arrtmp[75].ToString(); RM692D9_0B13.Text = arrtmp[77].ToString();
                    RM692D9_0R14.Text = arrtmp[79].ToString(); RM692D9_0G14.Text = arrtmp[81].ToString(); RM692D9_0B14.Text = arrtmp[83].ToString();
                    RM692D9_0R15.Text = arrtmp[85].ToString(); RM692D9_0G15.Text = arrtmp[87].ToString(); RM692D9_0B15.Text = arrtmp[89].ToString();
                    //RM692D9_0R15.Text = arrtmp[91].ToString(); RM692D9_0G15.Text = arrtmp[93].ToString(); RM692D9_0B15.Text = arrtmp[95].ToString();

                }
                else if (radioButtonbd31.Checked)
                {
                    RM692D9_0R1.Text = arrtmp[7].ToString(); RM692D9_0G1.Text = arrtmp[9].ToString(); RM692D9_0B1.Text = arrtmp[11].ToString();
                    RM692D9_0R2.Text = arrtmp[13].ToString(); RM692D9_0G2.Text = arrtmp[15].ToString(); RM692D9_0B2.Text = arrtmp[17].ToString();
                    RM692D9_0R3.Text = arrtmp[19].ToString(); RM692D9_0G3.Text = arrtmp[21].ToString(); RM692D9_0B3.Text = arrtmp[23].ToString();
                    RM692D9_0R4.Text = arrtmp[25].ToString(); RM692D9_0G4.Text = arrtmp[27].ToString(); RM692D9_0B4.Text = arrtmp[29].ToString();
                    RM692D9_0R5.Text = arrtmp[31].ToString(); RM692D9_0G5.Text = arrtmp[33].ToString(); RM692D9_0B5.Text = arrtmp[35].ToString();
                    RM692D9_0R6.Text = arrtmp[37].ToString(); RM692D9_0G6.Text = arrtmp[39].ToString(); RM692D9_0B6.Text = arrtmp[41].ToString();
                    RM692D9_0R7.Text = arrtmp[43].ToString(); RM692D9_0G7.Text = arrtmp[45].ToString(); RM692D9_0B7.Text = arrtmp[47].ToString();
                    RM692D9_0R8.Text = arrtmp[49].ToString(); RM692D9_0G8.Text = arrtmp[51].ToString(); RM692D9_0B8.Text = arrtmp[53].ToString();
                    RM692D9_0R9.Text = arrtmp[55].ToString(); RM692D9_0G9.Text = arrtmp[57].ToString(); RM692D9_0B9.Text = arrtmp[59].ToString();
                    RM692D9_0R10.Text = arrtmp[61].ToString(); RM692D9_0G10.Text = arrtmp[63].ToString(); RM692D9_0B10.Text = arrtmp[65].ToString();
                    RM692D9_0R11.Text = arrtmp[67].ToString(); RM692D9_0G11.Text = arrtmp[69].ToString(); RM692D9_0B11.Text = arrtmp[71].ToString();
                    RM692D9_0R12.Text = arrtmp[73].ToString(); RM692D9_0G12.Text = arrtmp[75].ToString(); RM692D9_0B12.Text = arrtmp[77].ToString();
                    RM692D9_0R13.Text = arrtmp[79].ToString(); RM692D9_0G13.Text = arrtmp[81].ToString(); RM692D9_0B13.Text = arrtmp[83].ToString();
                    RM692D9_0R14.Text = arrtmp[85].ToString(); RM692D9_0G14.Text = arrtmp[87].ToString(); RM692D9_0B14.Text = arrtmp[89].ToString();
                    RM692D9_0R15.Text = arrtmp[91].ToString(); RM692D9_0G15.Text = arrtmp[93].ToString(); RM692D9_0B15.Text = arrtmp[95].ToString();
                    RM692D9_0R16.Text = arrtmp[97].ToString(); RM692D9_0G16.Text = arrtmp[99].ToString(); RM692D9_0B16.Text = arrtmp[101].ToString();
                    RM692D9_0R17.Text = arrtmp[103].ToString(); RM692D9_0G17.Text = arrtmp[105].ToString(); RM692D9_0B17.Text = arrtmp[107].ToString();
                    RM692D9_0R18.Text = arrtmp[109].ToString(); RM692D9_0G18.Text = arrtmp[111].ToString(); RM692D9_0B18.Text = arrtmp[113].ToString();
                    RM692D9_0R19.Text = arrtmp[115].ToString(); RM692D9_0G19.Text = arrtmp[117].ToString(); RM692D9_0B19.Text = arrtmp[119].ToString();
                    RM692D9_0R20.Text = arrtmp[121].ToString(); RM692D9_0G20.Text = arrtmp[123].ToString(); RM692D9_0B20.Text = arrtmp[125].ToString();
                    RM692D9_0R21.Text = arrtmp[127].ToString(); RM692D9_0G21.Text = arrtmp[129].ToString(); RM692D9_0B21.Text = arrtmp[131].ToString();
                    RM692D9_0R22.Text = arrtmp[133].ToString(); RM692D9_0G22.Text = arrtmp[135].ToString(); RM692D9_0B22.Text = arrtmp[137].ToString();
                    RM692D9_0R23.Text = arrtmp[139].ToString(); RM692D9_0G23.Text = arrtmp[141].ToString(); RM692D9_0B23.Text = arrtmp[143].ToString();
                    RM692D9_0R24.Text = arrtmp[145].ToString(); RM692D9_0G24.Text = arrtmp[147].ToString(); RM692D9_0B24.Text = arrtmp[149].ToString();
                    RM692D9_0R25.Text = arrtmp[151].ToString(); RM692D9_0G25.Text = arrtmp[153].ToString(); RM692D9_0B25.Text = arrtmp[155].ToString();
                    RM692D9_0R26.Text = arrtmp[157].ToString(); RM692D9_0G26.Text = arrtmp[159].ToString(); RM692D9_0B26.Text = arrtmp[161].ToString();
                    RM692D9_0R27.Text = arrtmp[163].ToString(); RM692D9_0G27.Text = arrtmp[165].ToString(); RM692D9_0B27.Text = arrtmp[167].ToString();
                    RM692D9_0R28.Text = arrtmp[169].ToString(); RM692D9_0G28.Text = arrtmp[171].ToString(); RM692D9_0B28.Text = arrtmp[173].ToString();
                    RM692D9_0R29.Text = arrtmp[175].ToString(); RM692D9_0G29.Text = arrtmp[177].ToString(); RM692D9_0B29.Text = arrtmp[179].ToString();
                    RM692D9_0R30.Text = arrtmp[181].ToString(); RM692D9_0G30.Text = arrtmp[183].ToString(); RM692D9_0B30.Text = arrtmp[185].ToString();
                    RM692D9_0R31.Text = arrtmp[187].ToString(); RM692D9_0G31.Text = arrtmp[189].ToString(); RM692D9_0B31.Text = arrtmp[191].ToString();
                }


            }

        }
        void auto_gamma_write(UInt16 addr, UInt16 datar, UInt16 addg, UInt16 datag, UInt16 addb, UInt16 Datab)//一组gamma写入
        {
            Send_buf = new byte[17 + 1];
            Setting.TempRecord.cmdLen = 18;

            if ((datar < 0x000f) || (datag < 0x000f) || (Datab < 0x000f) || (datar > 0x0ff0) || (datag > 0x0ff0) || (Datab > 0x0ff0))
            {
                T5.Enabled = false;

                flag1 = 1;
                flag2 = 1;
                flag3 = 1;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                textBox1.Text += "写入数据检查：数值超出界限，请注意\r\n";
                textBox1.Select(textBox1.Text.Length, 0);
                textBox1.ScrollToCaret();
            }

            Send_buf[0] = 0x68;//RM692D9
            Send_buf[1] = 0xdd;//AUTO Gamma 标志位
            Send_buf[2] = 12 + 1;  //有效数据长度，新增一个供上位机传输额外控制命令：通知下位机是否强切指定PAGE
            Send_buf[3] = Convert.ToByte(addr >> 8);
            Send_buf[4] = Convert.ToByte(datar >> 8);
            Send_buf[5] = Convert.ToByte(addr & 0xff);
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(addg >> 8);
            Send_buf[8] = Convert.ToByte(datag >> 8);
            Send_buf[9] = Convert.ToByte(addg & 0xff);
            Send_buf[10] = Convert.ToByte(datag & 0xff);
            Send_buf[11] = Convert.ToByte(addb >> 8);
            Send_buf[12] = Convert.ToByte(Datab >> 8);
            Send_buf[13] = Convert.ToByte(addb & 0xff);
            Send_buf[14] = Convert.ToByte(Datab & 0xff);
            //if (chkNoSetPage.Checked)
            Send_buf[14 + 1] = 0x80; //新增指示下位机是否强切page
            CRCData.CalculateCrc16(Send_buf, out Send_buf[15 + 1], out Send_buf[16 + 1]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 17 + 1);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }
        //btnSaverdGamma

        private void btnSaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    //if (MainForm.IC_comboBox.SelectedIndex == 20-1)
                    if (radioButtonbd15.Checked)
                    {
                        str = str + "R1," + RM692D9_0R1.Text + "," + "G1," + RM692D9_0G1.Text + "," + "B1," + RM692D9_0B1.Text + "\r\n";
                        str = str + "R2," + RM692D9_0R2.Text + "," + "G2," + RM692D9_0G2.Text + "," + "B2," + RM692D9_0B2.Text + "\r\n";
                        str = str + "R3," + RM692D9_0R3.Text + "," + "G3," + RM692D9_0G3.Text + "," + "B3," + RM692D9_0B3.Text + "\r\n";
                        str = str + "R4," + RM692D9_0R4.Text + "," + "G4," + RM692D9_0G4.Text + "," + "B4," + RM692D9_0B4.Text + "\r\n";
                        str = str + "R5," + RM692D9_0R5.Text + "," + "G5," + RM692D9_0G5.Text + "," + "B5," + RM692D9_0B5.Text + "\r\n";
                        str = str + "R6," + RM692D9_0R6.Text + "," + "G6," + RM692D9_0G6.Text + "," + "B6," + RM692D9_0B6.Text + "\r\n";
                        str = str + "R7," + RM692D9_0R7.Text + "," + "G7," + RM692D9_0G7.Text + "," + "B7," + RM692D9_0B7.Text + "\r\n";
                        str = str + "R8," + RM692D9_0R8.Text + "," + "G8," + RM692D9_0G8.Text + "," + "B8," + RM692D9_0B8.Text + "\r\n";
                        str = str + "R9," + RM692D9_0R9.Text + "," + "G9," + RM692D9_0G9.Text + "," + "B9," + RM692D9_0B9.Text + "\r\n";
                        str = str + "R10," + RM692D9_0R10.Text + "," + "G10," + RM692D9_0G10.Text + "," + "B10," + RM692D9_0B10.Text + "\r\n";
                        str = str + "R11," + RM692D9_0R11.Text + "," + "G11," + RM692D9_0G11.Text + "," + "B11," + RM692D9_0B11.Text + "\r\n";
                        str = str + "R12," + RM692D9_0R12.Text + "," + "G12," + RM692D9_0G12.Text + "," + "B12," + RM692D9_0B12.Text + "\r\n";
                        str = str + "R13," + RM692D9_0R13.Text + "," + "G13," + RM692D9_0G13.Text + "," + "B13," + RM692D9_0B13.Text + "\r\n";
                        str = str + "R14," + RM692D9_0R14.Text + "," + "G14," + RM692D9_0G14.Text + "," + "B14," + RM692D9_0B14.Text + "\r\n";
                        str = str + "R15," + RM692D9_0R15.Text + "," + "G15," + RM692D9_0G15.Text + "," + "B15," + RM692D9_0B15.Text + "\r\n";
                        sw.WriteLine(str);
                    }
                    else if (radioButtonbd31.Checked)
                    {
                        //str = str + "保持兼容用R1," + RM692D9_R1.Text + "," + "保持兼容用G1," + RM692D9_G1.Text + "," + "保持兼容用B1," + RM692D9_B1.Text + "\r\n";
                        str = str + "R1," + RM692D9_0R1.Text + "," + "G1," + RM692D9_0G1.Text + "," + "B1," + RM692D9_0B1.Text + "\r\n";
                        str = str + "R2," + RM692D9_0R2.Text + "," + "G2," + RM692D9_0G2.Text + "," + "B2," + RM692D9_0B2.Text + "\r\n";
                        str = str + "R3," + RM692D9_0R3.Text + "," + "G3," + RM692D9_0G3.Text + "," + "B3," + RM692D9_0B3.Text + "\r\n";
                        str = str + "R4," + RM692D9_0R4.Text + "," + "G4," + RM692D9_0G4.Text + "," + "B4," + RM692D9_0B4.Text + "\r\n";
                        str = str + "R5," + RM692D9_0R5.Text + "," + "G5," + RM692D9_0G5.Text + "," + "B5," + RM692D9_0B5.Text + "\r\n";
                        str = str + "R6," + RM692D9_0R6.Text + "," + "G6," + RM692D9_0G6.Text + "," + "B6," + RM692D9_0B6.Text + "\r\n";
                        str = str + "R7," + RM692D9_0R7.Text + "," + "G7," + RM692D9_0G7.Text + "," + "B7," + RM692D9_0B7.Text + "\r\n";
                        str = str + "R8," + RM692D9_0R8.Text + "," + "G8," + RM692D9_0G8.Text + "," + "B8," + RM692D9_0B8.Text + "\r\n";
                        str = str + "R9," + RM692D9_0R9.Text + "," + "G9," + RM692D9_0G9.Text + "," + "B9," + RM692D9_0B9.Text + "\r\n";
                        str = str + "R10," + RM692D9_0R10.Text + "," + "G10," + RM692D9_0G10.Text + "," + "B10," + RM692D9_0B10.Text + "\r\n";
                        str = str + "R11," + RM692D9_0R11.Text + "," + "G11," + RM692D9_0G11.Text + "," + "B11," + RM692D9_0B11.Text + "\r\n";
                        str = str + "R12," + RM692D9_0R12.Text + "," + "G12," + RM692D9_0G12.Text + "," + "B12," + RM692D9_0B12.Text + "\r\n";
                        str = str + "R13," + RM692D9_0R13.Text + "," + "G13," + RM692D9_0G13.Text + "," + "B13," + RM692D9_0B13.Text + "\r\n";
                        str = str + "R14," + RM692D9_0R14.Text + "," + "G14," + RM692D9_0G14.Text + "," + "B14," + RM692D9_0B14.Text + "\r\n";
                        str = str + "R15," + RM692D9_0R15.Text + "," + "G15," + RM692D9_0G15.Text + "," + "B15," + RM692D9_0B15.Text + "\r\n";
                        str = str + "R16," + RM692D9_0R16.Text + "," + "G16," + RM692D9_0G16.Text + "," + "B16," + RM692D9_0B16.Text + "\r\n";
                        str = str + "R17," + RM692D9_0R17.Text + "," + "G17," + RM692D9_0G17.Text + "," + "B17," + RM692D9_0B17.Text + "\r\n";
                        str = str + "R18," + RM692D9_0R18.Text + "," + "G18," + RM692D9_0G18.Text + "," + "B18," + RM692D9_0B18.Text + "\r\n";
                        str = str + "R19," + RM692D9_0R19.Text + "," + "G19," + RM692D9_0G19.Text + "," + "B19," + RM692D9_0B19.Text + "\r\n";
                        str = str + "R20," + RM692D9_0R20.Text + "," + "G20," + RM692D9_0G20.Text + "," + "B20," + RM692D9_0B20.Text + "\r\n";
                        str = str + "R21," + RM692D9_0R21.Text + "," + "G21," + RM692D9_0G21.Text + "," + "B21," + RM692D9_0B21.Text + "\r\n";
                        str = str + "R22," + RM692D9_0R22.Text + "," + "G22," + RM692D9_0G22.Text + "," + "B22," + RM692D9_0B22.Text + "\r\n";
                        str = str + "R23," + RM692D9_0R23.Text + "," + "G23," + RM692D9_0G23.Text + "," + "B23," + RM692D9_0B23.Text + "\r\n";
                        str = str + "R24," + RM692D9_0R24.Text + "," + "G24," + RM692D9_0G24.Text + "," + "B24," + RM692D9_0B24.Text + "\r\n";
                        str = str + "R25," + RM692D9_0R25.Text + "," + "G25," + RM692D9_0G25.Text + "," + "B25," + RM692D9_0B25.Text + "\r\n";
                        str = str + "R26," + RM692D9_0R26.Text + "," + "G26," + RM692D9_0G26.Text + "," + "B26," + RM692D9_0B26.Text + "\r\n";
                        str = str + "R27," + RM692D9_0R27.Text + "," + "G27," + RM692D9_0G27.Text + "," + "B27," + RM692D9_0B27.Text + "\r\n";
                        str = str + "R28," + RM692D9_0R28.Text + "," + "G28," + RM692D9_0G28.Text + "," + "B28," + RM692D9_0B28.Text + "\r\n";
                        str = str + "R29," + RM692D9_0R29.Text + "," + "G29," + RM692D9_0G29.Text + "," + "B29," + RM692D9_0B29.Text + "\r\n";
                        str = str + "R30," + RM692D9_0R30.Text + "," + "G30," + RM692D9_0G30.Text + "," + "B30," + RM692D9_0B30.Text + "\r\n";
                        str = str + "R31," + RM692D9_0R31.Text + "," + "G31," + RM692D9_0G31.Text + "," + "B31," + RM692D9_0B31.Text + "\r\n";
                        sw.WriteLine(str);
                    }
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        }







        private void btnCleantextbox1_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }
        //浏览图片
        string defaultfilePath = "";
        string path = "";
        /// <summary>
        /// 递归文件夹，把文件夹里所有后缀类似图片的文件，加入listbox
        /// </summary>
        /// <param name="di"></param>
        System.DateTime TimeNow2 = new DateTime();
        TimeSpan TimeCount2 = new TimeSpan();
        public class MyImage
        {
            public MyImage(string p)
            {
                this.Path = p;
            }
            public string Path { get; set; }
            public override string ToString()
            {
                if (Path == null)
                {
                    return null;
                }
                return System.IO.Path.GetFileName(this.Path);
            }
        }


        private void btnReInit_Click(object sender, EventArgs e)
        {
            MainForm.btnRun_Click(MainForm.btnRun, e);
        }

        //private void KPQ_Demura_Help_Click(object sender, EventArgs e)
        //{
        //    MessageBoxTimeoutA((IntPtr )0,"EN-->擦除-->进入-->刷填", "2K平台Demura烧录流程",32,0,19000);            
        //}

        public string[] DDICInitCmdarray;   //DDICInitCmdarray保存脱水后的DriverCode
        private void txtProcessSourcecode(string originDriverCode, out string txtbaseClearOriginal, out string txtMCleatOriginal)
        {
            string filter = "\r\n";
            string TxtSourceCode = originDriverCode;
            TxtSourceCode = TxtSourceCode.Replace("-", ",");
            TxtSourceCode = TxtSourceCode.Replace(" ", "");
            TxtSourceCode = TxtSourceCode.Replace("=", ",");
            TxtSourceCode = TxtSourceCode.Replace(".", ",");
            TxtSourceCode = TxtSourceCode.Replace("DELAY", "delay");//转换为小写，防止被去除
            TxtSourceCode = TxtSourceCode.Replace("0x", "");
            TxtSourceCode = TxtSourceCode.Trim();
            TxtSourceCode = TxtSourceCode.Replace(filter, " ");

            string[] arr1 = TxtSourceCode.Trim().Split(' ');
            string[] arr2 = TxtSourceCode.Trim().Split(' ');
            for (int i = 0; i < arr1.Length; i++)
            {
                int tep = arr1[i].IndexOf("/");
                switch (tep)
                {
                    case 0:
                        {
                            //首字母为‘/’，arr1则保留,为清洁版使用，去除特殊字符，保留注释
                            arr2[i] = "";  //arr2为脱水版使用，只保留有效代码，将特殊字符与注释全部去除
                        }
                        break;
                    case (-1):
                        {
                            //不含有‘/’
                            //去除字符串中的特殊字符
                            arr1[i] = Regex.Replace(arr1[i], "[^0-9A-Fdelay,.;/-=\r\n]", "");
                            arr1[i] = arr1[i].Trim().TrimEnd(",./:=".ToCharArray());
                            arr2[i] = Regex.Replace(arr2[i], "[^0-9A-Fdelay,.;/-=\r\n]", "");
                            arr2[i] = arr2[i].Trim().TrimEnd(",./:=".ToCharArray());
                        }
                        break;
                    default:
                        {
                            arr2[i] = arr2[i].Substring(0, tep);
                            arr2[i] = Regex.Replace(arr2[i], "[^0-9A-Fdelay,.;/-=\r\n]", "");
                        }
                        break;

                }
            }
            string abc1 = string.Join("\r\n", arr1);
            string abc2 = string.Join("\r\n", arr2);
            txtbaseClearOriginal = abc1;
            txtMCleatOriginal = abc2;

        }

        private void Send_Cmd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            switch (btn.Name)
            {
                case "btnSend1":
                    {
                        MainForm.txtProcessDrivercode(txtCmd1.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
                case "btnSend2":
                    {
                        MainForm.txtProcessDrivercode(txtCmd2.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
                case "btnSend3":
                    {
                        MainForm.txtProcessDrivercode(txtCmd3.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
                case "btnSend4":
                    {
                        MainForm.txtProcessDrivercode(txtCmd4.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;

            }
            Setting.TempRecord.cmdLen = 6;
            if (Write_LP.Checked)
            {
                MainForm.SendCmd(0);
            }
            else
            {
                MainForm.SendCmd(4);
            }
            textBox1.Text += "命令已下发...\r\n";
            textBox1.Select(textBox1.Text.Length, 0);
            textBox1.ScrollToCaret();
        }

        private void OpenTxtCode_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            of.Title = "初始化 Code";

            if (of.ShowDialog() == DialogResult.OK)
            {
                StreamReader streamReader = new StreamReader(of.FileName, Encoding.Default);
                switch (btn.Name)
                {
                    case "OpenTxtCode":
                        {
                            txtCmd1.Clear();
                            txtCmd1.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode2":
                        {
                            txtCmd2.Clear();
                            txtCmd2.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode3":
                        {
                            txtCmd3.Clear();
                            txtCmd3.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode4":
                        {
                            txtCmd4.Clear();
                            txtCmd4.Text = streamReader.ReadToEnd();
                        }
                        break;
                    default: break;
                }
                streamReader.Close();
            }
        }

        private void btnSaveDriverCode_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            //sfd.InitialDirectory = @"C:\Users\SpringRain\Desktop";
            sfd.Title = "请选择要保存的文件路径";
            sfd.Filter = "文本文件|*.txt|所有文件|*.*";
            sfd.ShowDialog();

            //获得用户要保存的文件的路径
            string path = sfd.FileName;
            if (path == "")
            {
                return;
            }
            using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                byte[] buffer = Encoding.Default.GetBytes(txtCmd1.Text);
                fsWrite.Write(buffer, 0, buffer.Length);
            }
            MessageBox.Show("保存成功");
        }
        private void SaveCode_Click(object sender, EventArgs e)
        {
            SaveFileDialog txt = new SaveFileDialog();
            txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            string str = "//-----保存Driver IC Code文件------\r\n";
            if (txt.ShowDialog() == DialogResult.OK)
            {
                if (txt.FileName == " ")
                {
                    return;
                }
                StreamWriter sw = null; //文件流
                sw = new StreamWriter(txt.FileName, false, Encoding.Default);//Encoding.GetEncoding("UTF-8")
                try
                {
                    if (ChkCMD1.Checked)
                    {
                        str += "//---CMD1---\r\n" + txtCmd1.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD2.Checked)
                    {
                        str += "//---CMD2---\r\n" + txtCmd2.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD3.Checked)
                    {
                        str += "//---CMD3---\r\n" + txtCmd3.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD4.Checked)
                    {
                        str += "//---CMD4---\r\n" + txtCmd4.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }

                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Driver IC Code保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Driver IC Code保存时发生错误，请再试！", "系统提示：");
                }
            }

        }


        private void RM692D9_692_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible == true) //子窗体已开启标识
            {
                if (MainForm.comPort.IsOpen)
                {
                    MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);
                    Thread.Sleep(5);
                    MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
                    Thread.Sleep(5);
                    textBox1.Text += "com 已经连接！\r\n";
                }
            }
            else
            {
                //MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);

            }
        }

        private void RM692D9_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            btnBack_Click(btnBack, e);
        }
        #endregion



        //GIR & LIR
        //GIR
        //实时显示310控件显示界面
        private void RM692D9_692_IR_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x3F;        //RM692D9
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                //pengcs
                case "btn_GIR_Start":
                    { Send_buf[1] = 0x0C; }//与下位机同时改 GIR_Drop_Start
                    break;
                case "btn_GIR_Stop":
                    { Send_buf[1] = 0x0D; }//与下位机同时改 GIR_Drop_Stop
                    break;
                case "btn_LIR_Start":
                    { Send_buf[1] = 0x10; }//与下位机同时改
                    break;
                case "btn_LIR_Stop":
                    { Send_buf[1] = 0x11; }//与下位机同时改
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }


        private bool loadbmpfile(string bmppath)
        {
            Image imgBack = System.Drawing.Image.FromFile(bmppath);
            Bitmap loadbmp = (Bitmap)imgBack;
            MemoryStream aFile;
            aFile = new MemoryStream();
            loadbmp.Save(aFile, System.Drawing.Imaging.ImageFormat.Bmp);
            aFile.Seek(0, SeekOrigin.Begin);//            aFile.Position = 0;             
            Int32 HACT_True;
            Int32 VACT_True;
            Int32 m_txlen = 0;
            Int32 j;
            //FileStream aFile;
            byte[] TemFileContent = new byte[1024 * 100];
            System.Int32 ByteReaded = 0x00;
            #region
            //------------20180808---------------
            try
            { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                MainForm.comPort.Write(TemFileContent, 0, 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                imgBack.Dispose();
                loadbmp.Dispose();
                aFile.Dispose();
                return false;
            }
            //-----------------------------------
            #endregion
            try
            {
                ByteReaded = aFile.Read(TemFileContent, 0, 54);
                HACT_True = (Convert.ToInt32(TemFileContent[19]) << 8) + TemFileContent[18];
                VACT_True = Convert.ToInt32((TemFileContent[23] << 8) + TemFileContent[22]);
                j = 1;
                {
                    aFile.Seek(54, SeekOrigin.Begin);
                    while (j <= VACT_True)
                    {
                        if (HACT_True % 4 != 0)
                            aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                        else
                            aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);

                        ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                        try
                        {
                            MainForm.comPort.Write(TemFileContent, 0, ByteReaded);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            imgBack.Dispose();
                            loadbmp.Dispose();
                            aFile.Dispose();
                            break;
                        }
                        m_txlen += ByteReaded;
                        // Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        j++;
                    }
                    TemFileContent[0] = 0x5A;
                    try
                    {
                        MainForm.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        imgBack.Dispose();
                        loadbmp.Dispose();
                        aFile.Dispose();
                        return false;
                    }
                    aFile.Close();
                }
                imgBack.Dispose();
                loadbmp.Dispose();
                aFile.Dispose();
                return true;
            }
            catch
            {
                textBox1.Text += "啥图片都没显示";
                textBox1.Select(textBox1.Text.Length, 0);
                textBox1.ScrollToCaret();
                imgBack.Dispose();
                loadbmp.Dispose();
                aFile.Dispose();
                return false;
            }

        }

        private void ILI7835_Gamma_Tuning(Object myObject, EventArgs e)
        {
            int total_points = Diven_Gamma[0].Points_num;
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            Thread.Sleep(10);
            //MainForm.Msr_CASDK();
            //Msr_CA310();
            MainForm.Msr_CASDK();
            auto_lum = MainForm.CASDK_LUM;
            auto_x = MainForm.CASDK_x;
            auto_y = MainForm.CASDK_y;

            //textBox_LV.Text = Convert.ToString(auto_lum);
            //textBox_X.Text = Convert.ToString(auto_x);
            //textBox_Y.Text = Convert.ToString(auto_y);
            textBox_LV.Text = auto_lum.ToString("#0.000000");
            textBox_X.Text = auto_x.ToString("#0.000000");
            textBox_Y.Text = auto_y.ToString("#0.000000");

            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((auto_lum < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (auto_lum > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                { flag1 = 0; }
                else
                { flag1 = 1; }
                if ((auto_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (auto_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                { flag3 = 0; }
                else
                { flag3 = 1; }
                if ((auto_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (auto_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                { flag2 = 0; }
                else
                { flag2 = 1; }

            }
            if ((index == 0) && (temptest255level == false))
            {
                flag1 = 1;
                temptest255level = true;
            }
            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                MainForm.Gamma_auto_adjust_count = 0;
                //--------
                index++;

                if (index >= 11)
                {
                    if (index == 13)
                    {
                        if (RM692D9_0R4.Value < RM692D9_0R3.Value)
                        {
                            RM692D9_0R3.Value = RM692D9_0R4.Value - 0x1F;
                        }

                        if (RM692D9_0G4.Value < RM692D9_0G3.Value)
                        {
                            RM692D9_0G3.Value = RM692D9_0G4.Value - 0x1F;
                        }

                        if (RM692D9_0B4.Value < RM692D9_0B3.Value)
                        {
                            RM692D9_0B3.Value = RM692D9_0B4.Value - 0x1F;
                        }

                    }
                    else if (index == 14)
                    {
                        if (RM692D9_0R3.Value < RM692D9_0R2.Value)
                        {
                            RM692D9_0R2.Value = RM692D9_0R3.Value - 0x0F;
                        }

                        if (RM692D9_0G3.Value < RM692D9_0G2.Value)
                        {
                            RM692D9_0G2.Value = RM692D9_0G3.Value - 0x0F;
                        }

                        if (RM692D9_0B3.Value < RM692D9_0B2.Value)
                        {
                            RM692D9_0B2.Value = RM692D9_0B3.Value - 0x0F;
                        }
                    }


                }

                if ((index == total_points) || (chkgirgamma1.Checked) || (chkGIRW255.Checked))
                {
                    textBox1.Text = textBox1.Text + "RM692D9 Gamma调试完毕!\r\n";
                }
                else if (index < total_points)
                {

                    //if (radioButtonbd15.Checked)
                    //{
                    //    numPatternR.Value = MainForm.numPatternR.Value = Point_692AXDefault_sll[index];//固定255灰阶
                    //    numPatternG.Value = MainForm.numPatternG.Value = Point_692AXDefault_sll[index];
                    //    numPatternB.Value = MainForm.numPatternB.Value = Point_692AXDefault_sll[index];
                    //}
                    //else
                    {
                        numPatternR.Value = MainForm.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                        numPatternG.Value = MainForm.numPatternG.Value = Diven_Offset[index].NO;
                        numPatternB.Value = MainForm.numPatternB.Value = Diven_Offset[index].NO;
                    }



                    Application.DoEvents();
                    MainForm.OLED_Show_Click(MainForm.btnPatternW, null);//写入Pattern参数
                    Thread.Sleep(30); //1秒=1000毫秒，


                    if (Page58.Checked == true || checkBox3.Checked == true)
                    {
                        bool load_bmp_status = false;
                        string bmppath = "";
                        if (Page58.Checked == true)
                        {
                            bmppath = strcurrentpath + "/udcgammabmp/31/" + bmpname31[index];
                        }
                        else if (checkBox3.Checked == true)
                        {
                            bmppath = strcurrentpath + "\\aodgammabmp\\" + aodbmpname[index];
                        }

                        if (!File.Exists(bmppath))
                        {
                            MessageBox.Show(bmpname[index] + "图片不存在");
                            return;
                        }

                        load_bmp_status = loadbmpfile(bmppath);
                        if (!load_bmp_status)
                        {
                            MessageBox.Show(bmpname[index] + "图片加载失败");
                            return;
                        }
                    }

                    //Thread.Sleep(50); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;

                    //Application.DoEvents();
                    //MainForm.OLED_Show_Click(MainForm.btnPatternW, null);//写入Pattern参数
                    //Thread.Sleep(50); //1秒=1000毫秒，
                }
            }
            else
            {
                decimal r = 0, g = 0, b = 0;
                CaSDKObj.YUV_to_RGB(auto_y, auto_x, out Lr_t, out Lb_t, out Lg_t);



                //lumr = MainForm.CASDK_LUM * Lr_t;
                //lumg = MainForm.CASDK_LUM * Lg_t;
                //lumb = MainForm.CASDK_LUM * Lb_t;

                lumr = auto_lum * Lr_t;
                lumg = auto_lum * Lg_t;
                lumb = auto_lum * Lb_t;

                try
                {
                    switch (index)
                    {
                        #region//
                        case 14:   //0
                            {
                                //public void Flexible_Gamma_auto_Adujst(
                                //int lum_adjust_enable, int x_adjust_enable, int y_adjust_enable,
                                //double measure_lum, double measure_x, double measure_y,
                                //double target_lum, double target_x, double target_y,
                                //decimal input_reg_red, decimal input_reg_green, decimal input_reg_blue,
                                //int gary_target, int reg_range,
                                ////output
                                //out decimal output_reg_red, out decimal output_reg_green, out decimal output_reg_blue, out bool timeout_flag)

                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R1.Value, RM692D9_0G1.Value, RM692D9_0B1.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R1.Value = r = 0;
                                RM692D9_0G1.Value = g = 0;
                                RM692D9_0B1.Value = b = 0;
                            }
                            break;
                        case 13:    //1
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R2.Value, RM692D9_0G2.Value, RM692D9_0B2.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                //if (Gamma_adjust_timeout_flag == true)
                                {
                                    if ((deR1 % 2) == 1)
                                    {
                                        deR1 = deR1 + 1;
                                    }
                                    if ((deG1 % 2) == 1)
                                    {
                                        deG1 = deG1 + 1;
                                    }
                                    if ((deB1 % 2) == 1)
                                    {
                                        deB1 = deB1 + 1;
                                    }
                                    Gamma_adjust_timeout_flag = true;
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R2.Value = r = deR1 / 2;
                                RM692D9_0G2.Value = g = deG1 / 2;
                                RM692D9_0B2.Value = b = deB1 / 2;


                            }
                            break;

                        case 12:   //3
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R3.Value, RM692D9_0G3.Value, RM692D9_0B3.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                deR1 = RM692D9_0R3.Value = r;
                                deG1 = RM692D9_0G3.Value = g;
                                deB1 = RM692D9_0B3.Value = b;

                            }
                            break;

                        case 11:   //7
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R4.Value, RM692D9_0G4.Value, RM692D9_0B4.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R4.Value = r;
                                RM692D9_0G4.Value = g;
                                RM692D9_0B4.Value = b;

                            }
                            break;

                        case 10:  //16
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R5.Value, RM692D9_0G5.Value, RM692D9_0B5.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R5.Value = r;
                                RM692D9_0G5.Value = g;
                                RM692D9_0B5.Value = b;

                            }
                            break;

                        case 9:   //24
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R6.Value, RM692D9_0G6.Value, RM692D9_0B6.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R6.Value = r;
                                RM692D9_0G6.Value = g;
                                RM692D9_0B6.Value = b;


                            }
                            break;

                        case 8:    //32
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R7.Value, RM692D9_0G7.Value, RM692D9_0B7.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R7.Value = r;
                                RM692D9_0G7.Value = g;
                                RM692D9_0B7.Value = b;


                            }
                            break;

                        case 7:   //40
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R8.Value, RM692D9_0G8.Value, RM692D9_0B8.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R8.Value = r;
                                RM692D9_0G8.Value = g;
                                RM692D9_0B8.Value = b;


                            }
                            break;

                        case 6:    //64
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R9.Value, RM692D9_0G9.Value, RM692D9_0B9.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R9.Value = r;
                                RM692D9_0G9.Value = g;
                                RM692D9_0B9.Value = b;


                            }
                            break;

                        case 5:   //80
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R10.Value, RM692D9_0G10.Value, RM692D9_0B10.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R10.Value = r;
                                RM692D9_0G10.Value = g;
                                RM692D9_0B10.Value = b;


                            }
                            break;

                        case 4:  //112
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R11.Value, RM692D9_0G11.Value, RM692D9_0B11.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R11.Value = r;
                                RM692D9_0G11.Value = g;
                                RM692D9_0B11.Value = b;

                            }
                            break;

                        case 3:  //144
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R12.Value, RM692D9_0G12.Value, RM692D9_0B12.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R12.Value = r;
                                RM692D9_0G12.Value = g;
                                RM692D9_0B12.Value = b;
                            }
                            break;
                        case 2:  //208
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R13.Value, RM692D9_0G13.Value, RM692D9_0B13.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R13.Value = r;
                                RM692D9_0G13.Value = g;
                                RM692D9_0B13.Value = b;
                            }
                            break;
                        case 1:  //240
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R14.Value, RM692D9_0G14.Value, RM692D9_0B14.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R14.Value = r;
                                RM692D9_0G14.Value = g;
                                RM692D9_0B14.Value = b;

                            }
                            break;
                        case 0:   //255
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R15.Value, RM692D9_0G15.Value, RM692D9_0B15.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R15.Value = r;
                                RM692D9_0G15.Value = g;
                                RM692D9_0B15.Value = b;

                            }


                            break;
                        default:
                            break;

                            #endregion

                    }
                    #region//
                    if (index < 13)
                    {
                        if (Page58.Checked == true)
                        {
                            auto_gamma_write_RM692D9UDC((UInt16)r, (UInt16)g, (UInt16)b, index);
                        }
                        else
                        {
                            auto_gamma_write_RM692D9((UInt16)r, (UInt16)g, (UInt16)b, index);
                        }

                        Thread.Sleep(15);
                    }
                    else if (index == 13)
                    {
                        if (Page58.Checked == true)
                        {

                            auto_gamma_write_RM692D9UDC((UInt16)(deR1 / 2), (UInt16)(deG1 / 2), (UInt16)(deB1 / 2), index);
                        }
                        else
                        {
                            auto_gamma_write_RM692D9((UInt16)r, (UInt16)g, (UInt16)b, index);
                        }
                        Thread.Sleep(15);
                    }
                    else
                    {
                        if (Page58.Checked == true)
                        {
                            auto_gamma_write_RM692D9UDC(0, 0, 0, index);
                        }
                        else
                        {
                            auto_gamma_write_RM692D9(0, 0, 0, index);
                        }

                        Thread.Sleep(15);
                        RM692D9_Gamma_Click(buttonW_Red, e);
                        Thread.Sleep(15);
                        RM692D9_Gamma_Click(buttonW_Green, e);
                        Thread.Sleep(15);
                        RM692D9_Gamma_Click(buttonW_Blue, e);
                        Thread.Sleep(15);
                        T20.Enabled = false;
                        textBox1.Text = textBox1.Text + "RM692D9 Gamma调试完毕!\r\n";
                    }
                    #endregion
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("调整数值超限");
                }
            }
        }

        private void ILI7835_Gamma_Tuning31(Object myObject, EventArgs e)
        {
            int total_points = Diven_Gamma[0].Points_num;
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            Thread.Sleep(10);
            // MainForm.Msr_CASDK();
            // Msr_CA310();
            // MainForm.Msr_CASDK();
            //Msr_CA310();
            MainForm.Msr_CASDK();
            auto_lum = MainForm.CASDK_LUM;
            auto_x = MainForm.CASDK_x;
            auto_y = MainForm.CASDK_y;


            if (Gamma_adjust_timeout_flag == true)//如果是0绑点
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
            }
            else
            {
                if ((auto_lum < (D_lum[index] + D_lum[index] * Diven_Offset[index].LUM_OFF / 100)) && (auto_lum > (D_lum[index] - D_lum[index] * Diven_Offset[index].LUM_OFF / 100)))//G
                { flag1 = 0; }
                else
                { flag1 = 1; }
                if ((auto_x < (Diven_Gamma[0].X + Diven_Offset[index].X_OFF)) && (auto_x > (Diven_Gamma[0].X - Diven_Offset[index].X_OFF)) && flag1 == 0)//R
                { flag3 = 0; }
                else
                { flag3 = 1; }
                if ((auto_y < (Diven_Gamma[0].Y + Diven_Offset[index].Y_OFF)) && (auto_y > (Diven_Gamma[0].Y - Diven_Offset[index].Y_OFF)) && flag1 == 0 && flag3 == 0)//B
                { flag2 = 0; }
                else
                { flag2 = 1; }

            }
            if ((index == 0) && (temptest255level == false))
            {
                flag1 = 1;
                temptest255level = true;
            }
            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                MainForm.Gamma_auto_adjust_count = 0;
                //--------
                index++;

                if ((index == total_points) || (chkgirgamma1.Checked) || (chkGIRW255.Checked))
                {
                    textBox1.Text = textBox1.Text + "RM692D9 Gamma调试完毕!\r\n";
                }
                else if (index < total_points)
                {
                    numPatternR.Value = MainForm.numPatternR.Value = Diven_Offset[index].NO;//固定255灰阶
                    numPatternG.Value = MainForm.numPatternG.Value = Diven_Offset[index].NO;
                    numPatternB.Value = MainForm.numPatternB.Value = Diven_Offset[index].NO;

                    Application.DoEvents();
                    MainForm.OLED_Show_Click(MainForm.btnPatternW, null);//写入Pattern参数
                    Thread.Sleep(50); //1秒=1000毫秒，


                    //if(index == 1)
                    //{
                    //    Thread.Sleep(500); //1秒=1000毫秒，
                    //}
                    if (Page58.Checked == true || checkBox3.Checked == true)
                    {
                        bool load_bmp_status = false;
                        string bmppath = "";
                        if (Page58.Checked == true)
                        {
                            bmppath = strcurrentpath + "/udcgammabmp/31/" + bmpname31[index];
                        }
                        else if (checkBox3.Checked == true)
                        {
                            bmppath = strcurrentpath + "\\aodgammabmp\\" + aodbmpname[index];
                        }

                        if (!File.Exists(bmppath))
                        {
                            MessageBox.Show(bmpname[index] + "图片不存在");
                            return;
                        }

                        load_bmp_status = loadbmpfile(bmppath);
                        if (!load_bmp_status)
                        {
                            MessageBox.Show(bmpname[index] + "图片加载失败");
                            return;
                        }
                    }

                    Thread.Sleep(100); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;

                    //Application.DoEvents();
                    //MainForm.OLED_Show_Click(MainForm.btnPatternW, null);//写入Pattern参数
                    //Thread.Sleep(50); //1秒=1000毫秒，
                }
            }
            else
            {
                decimal r = 0, g = 0, b = 0;
                CaSDKObj.YUV_to_RGB(auto_y, auto_x, out Lr_t, out Lb_t, out Lg_t);

                lumr = auto_lum * Lr_t;
                lumg = auto_lum * Lg_t;
                lumb = auto_lum * Lb_t;

                try
                {
                    switch (index)
                    {
                        #region//
                        case 30:   //0
                            {
                                //public void Flexible_Gamma_auto_Adujst(
                                //int lum_adjust_enable, int x_adjust_enable, int y_adjust_enable,
                                //double measure_lum, double measure_x, double measure_y,
                                //double target_lum, double target_x, double target_y,
                                //decimal input_reg_red, decimal input_reg_green, decimal input_reg_blue,
                                //int gary_target, int reg_range,
                                ////output
                                //out decimal output_reg_red, out decimal output_reg_green, out decimal output_reg_blue, out bool timeout_flag)

                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R1.Value, RM692D9_0G1.Value, RM692D9_0B1.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R1.Value = r = 0;
                                RM692D9_0G1.Value = g = 0;
                                RM692D9_0B1.Value = b = 0;
                            }
                            break;
                        case 29:    //3
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R2.Value, RM692D9_0G2.Value, RM692D9_0B2.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                //if (Gamma_adjust_timeout_flag == true)
                                {
                                    if ((deR1 % 2) == 1)
                                    {
                                        deR1 = deR1 + 1;
                                    }
                                    if ((deG1 % 2) == 1)
                                    {
                                        deG1 = deG1 + 1;
                                    }
                                    if ((deB1 % 2) == 1)
                                    {
                                        deB1 = deB1 + 1;
                                    }
                                    Gamma_adjust_timeout_flag = true;
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R2.Value = r = deR1 / 2;
                                RM692D9_0G2.Value = g = deG1 / 2;
                                RM692D9_0B2.Value = b = deB1 / 2;


                            }
                            break;

                        case 28:   //4
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R3.Value, RM692D9_0G3.Value, RM692D9_0B3.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                deR1 = RM692D9_0R3.Value = r;
                                deG1 = RM692D9_0G3.Value = g;
                                deB1 = RM692D9_0B3.Value = b;

                            }
                            break;

                        case 27:   //8
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R4.Value, RM692D9_0G4.Value, RM692D9_0B4.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R4.Value = r;
                                RM692D9_0G4.Value = g;
                                RM692D9_0B4.Value = b;

                            }
                            break;

                        case 26:  //12
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R5.Value, RM692D9_0G5.Value, RM692D9_0B5.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R5.Value = r;
                                RM692D9_0G5.Value = g;
                                RM692D9_0B5.Value = b;

                            }
                            break;

                        case 25:   //16
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R6.Value, RM692D9_0G6.Value, RM692D9_0B6.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R6.Value = r;
                                RM692D9_0G6.Value = g;
                                RM692D9_0B6.Value = b;


                            }
                            break;

                        case 24:    //20
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R7.Value, RM692D9_0G7.Value, RM692D9_0B7.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R7.Value = r;
                                RM692D9_0G7.Value = g;
                                RM692D9_0B7.Value = b;


                            }
                            break;

                        case 23:   //28
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R8.Value, RM692D9_0G8.Value, RM692D9_0B8.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R8.Value = r;
                                RM692D9_0G8.Value = g;
                                RM692D9_0B8.Value = b;


                            }
                            break;

                        case 22:    //36
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R9.Value, RM692D9_0G9.Value, RM692D9_0B9.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R9.Value = r;
                                RM692D9_0G9.Value = g;
                                RM692D9_0B9.Value = b;


                            }
                            break;

                        case 21:   //44
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R10.Value, RM692D9_0G10.Value, RM692D9_0B10.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R10.Value = r;
                                RM692D9_0G10.Value = g;
                                RM692D9_0B10.Value = b;


                            }
                            break;

                        case 20:  //52
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R11.Value, RM692D9_0G11.Value, RM692D9_0B11.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R11.Value = r;
                                RM692D9_0G11.Value = g;
                                RM692D9_0B11.Value = b;

                            }
                            break;

                        case 19:  //68
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R12.Value, RM692D9_0G12.Value, RM692D9_0B12.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R12.Value = r;
                                RM692D9_0G12.Value = g;
                                RM692D9_0B12.Value = b;
                            }
                            break;
                        case 18:  //84
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R13.Value, RM692D9_0G13.Value, RM692D9_0B13.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R13.Value = r;
                                RM692D9_0G13.Value = g;
                                RM692D9_0B13.Value = b;
                            }
                            break;
                        case 17:  //100
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R14.Value, RM692D9_0G14.Value, RM692D9_0B14.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R14.Value = r;
                                RM692D9_0G14.Value = g;
                                RM692D9_0B14.Value = b;

                            }
                            break;
                        case 16:   //116
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R15.Value, RM692D9_0G15.Value, RM692D9_0B15.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R15.Value = r;
                                RM692D9_0G15.Value = g;
                                RM692D9_0B15.Value = b;

                            }
                            break;
                        case 15:   //132
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R16.Value, RM692D9_0G16.Value, RM692D9_0B16.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R16.Value = r;
                                RM692D9_0G16.Value = g;
                                RM692D9_0B16.Value = b;

                            }
                            break;
                        case 14:   //148
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R17.Value, RM692D9_0G17.Value, RM692D9_0B17.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R17.Value = r;
                                RM692D9_0G17.Value = g;
                                RM692D9_0B17.Value = b;

                            }
                            break;
                        case 13:   //164
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R18.Value, RM692D9_0G18.Value, RM692D9_0B18.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R18.Value = r;
                                RM692D9_0G18.Value = g;
                                RM692D9_0B18.Value = b;

                            }
                            break;
                        case 12:   //180
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R19.Value, RM692D9_0G19.Value, RM692D9_0B19.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R19.Value = r;
                                RM692D9_0G19.Value = g;
                                RM692D9_0B19.Value = b;

                            }
                            break;
                        case 11:   //188
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R20.Value, RM692D9_0G20.Value, RM692D9_0B20.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R20.Value = r;
                                RM692D9_0G20.Value = g;
                                RM692D9_0B20.Value = b;

                            }
                            break;
                        case 10:   //196
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R21.Value, RM692D9_0G21.Value, RM692D9_0B21.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R21.Value = r;
                                RM692D9_0G21.Value = g;
                                RM692D9_0B21.Value = b;

                            }
                            break;
                        case 9:   //204
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R22.Value, RM692D9_0G22.Value, RM692D9_0B22.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R22.Value = r;
                                RM692D9_0G22.Value = g;
                                RM692D9_0B22.Value = b;

                            }
                            break;
                        case 8:   //212
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R23.Value, RM692D9_0G23.Value, RM692D9_0B23.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R23.Value = r;
                                RM692D9_0G23.Value = g;
                                RM692D9_0B23.Value = b;

                            }
                            break;
                        case 7:   //220
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R24.Value, RM692D9_0G24.Value, RM692D9_0B24.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R24.Value = r;
                                RM692D9_0G24.Value = g;
                                RM692D9_0B24.Value = b;

                            }
                            break;
                        case 6:   //228
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R25.Value, RM692D9_0G25.Value, RM692D9_0B25.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R25.Value = r;
                                RM692D9_0G25.Value = g;
                                RM692D9_0B25.Value = b;

                            }
                            break;
                        case 5:   //236
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R26.Value, RM692D9_0G26.Value, RM692D9_0B26.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R26.Value = r;
                                RM692D9_0G26.Value = g;
                                RM692D9_0B26.Value = b;

                            }
                            break;
                        case 4:   //244
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R27.Value, RM692D9_0G27.Value, RM692D9_0B27.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R27.Value = r;
                                RM692D9_0G27.Value = g;
                                RM692D9_0B27.Value = b;

                            }
                            break;
                        case 3:   //248
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R28.Value, RM692D9_0G28.Value, RM692D9_0B28.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R28.Value = r;
                                RM692D9_0G28.Value = g;
                                RM692D9_0B28.Value = b;

                            }
                            break;
                        case 2:   //252
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R29.Value, RM692D9_0G29.Value, RM692D9_0B29.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R29.Value = r;
                                RM692D9_0G29.Value = g;
                                RM692D9_0B29.Value = b;

                            }
                            break;
                        case 1:   //254
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R30.Value, RM692D9_0G30.Value, RM692D9_0B30.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R30.Value = r;
                                RM692D9_0G30.Value = g;
                                RM692D9_0B30.Value = b;

                            }
                            break;
                        case 0:   //255
                            {
                                MainForm.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], RM692D9_0R31.Value, RM692D9_0G31.Value, RM692D9_0B31.Value, Diven_Offset[index].NO, 0xfff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                RM692D9_0R31.Value = r;
                                RM692D9_0G31.Value = g;
                                RM692D9_0B31.Value = b;

                            }
                            break;
                        default:
                            break;

                            #endregion

                    }
                    #region//
                    if (index < 29)
                    {
                        if (Page58.Checked == true)
                        {
                            auto_gamma_write_RM692D9UDC_31((UInt16)r, (UInt16)g, (UInt16)b, index);
                        }
                        else
                        {
                            auto_gamma_write_RM692D9_31((UInt16)r, (UInt16)g, (UInt16)b, index);
                        }


                        Thread.Sleep(10);
                    }
                    else if (index == 29)
                    {
                        if (Page58.Checked == true)
                        {

                            auto_gamma_write_RM692D9UDC_31((UInt16)(deR1 / 2), (UInt16)(deG1 / 2), (UInt16)(deB1 / 2), index);
                        }
                        else
                        {
                            auto_gamma_write_RM692D9_31((UInt16)r, (UInt16)g, (UInt16)b, index);
                        }
                        Thread.Sleep(10);
                    }
                    else
                    {
                        if (Page58.Checked == true)
                        {
                            auto_gamma_write_RM692D9UDC_31(0, 0, 0, index);
                        }
                        else
                        {
                            auto_gamma_write_RM692D9_31(0, 0, 0, index);
                        }

                        Thread.Sleep(15);
                        RM692D9_Gamma_Click(buttonW_Red, e);
                        Thread.Sleep(15);
                        RM692D9_Gamma_Click(buttonW_Green, e);
                        Thread.Sleep(15);
                        RM692D9_Gamma_Click(buttonW_Blue, e);
                        Thread.Sleep(15);
                        T20.Enabled = false;
                        textBox1.Text = textBox1.Text + "RM692D9 Gamma调试完毕!\r\n";
                    }
                    #endregion
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("调整数值超限");
                }
            }
        }


        public void auto_gamma_write_RM692D9(UInt16 datar, UInt16 datag, UInt16 Datab, int index_tep)//一组gamma写入
        {

            //获取当前的
            Send_buf = new byte[13];
            Setting.TempRecord.cmdLen = 13;
            Send_buf[0] = 0x68;//RM692D9
            Send_buf[1] = 0xdd;//AUTO Gamma 标志位
            Send_buf[2] = 8;  //有效数据长度，新增一个供上位机传输额外控制命令：通知下位机是否强切指定PAGE
            if (Page50.Checked)
            {
                Send_buf[3] = 0x01;    //  page 50
            }
            else if (Page52H.Checked)
            {
                Send_buf[3] = 0x02;    //page52
            }

            Send_buf[4] = (byte)(index_tep & 0xFF);      //发送哪个绑点  0代表255绑点.
            Send_buf[5] = Convert.ToByte(datar >> 8);       //r
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(datag >> 8);      //g
            Send_buf[8] = Convert.ToByte(datag & 0xff);
            Send_buf[9] = Convert.ToByte(Datab >> 8);      //b
            Send_buf[10] = Convert.ToByte(Datab & 0xff);

            CRCData.CalculateCrc16(Send_buf, out Send_buf[11], out Send_buf[12]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 13);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }

        public void auto_gamma_write_RM692D9_31(UInt16 datar, UInt16 datag, UInt16 Datab, int index_tep)//一组gamma写入
        {

            //获取当前的
            Send_buf = new byte[13];
            Setting.TempRecord.cmdLen = 13;
            Send_buf[0] = 0x68;//RM692D9
            Send_buf[1] = 0xfd;//AUTO Gamma 标志位
            Send_buf[2] = 8;  //有效数据长度，新增一个供上位机传输额外控制命令：通知下位机是否强切指定PAGE
            if (Page50.Checked)
            {
                Send_buf[3] = 0x01;    //  page 50
            }
            else if (Page52H.Checked)
            {
                Send_buf[3] = 0x02;    //page52
            }
            Send_buf[4] = (byte)(index_tep & 0xFF);      //发送哪个绑点  0代表255绑点.
            Send_buf[5] = Convert.ToByte(datar >> 8);       //r
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(datag >> 8);      //g
            Send_buf[8] = Convert.ToByte(datag & 0xff);
            Send_buf[9] = Convert.ToByte(Datab >> 8);      //b
            Send_buf[10] = Convert.ToByte(Datab & 0xff);

            CRCData.CalculateCrc16(Send_buf, out Send_buf[11], out Send_buf[12]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 13);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }

        public void auto_gamma_write_RM692D9UDC(UInt16 datar, UInt16 datag, UInt16 Datab, int index_tep)//一组gamma写入
        {

            //获取当前的
            Send_buf = new byte[13];
            Setting.TempRecord.cmdLen = 13;
            Send_buf[0] = 0x68;//RM692D9
            Send_buf[1] = 0xdc;//AUTO Gamma 标志位
            Send_buf[2] = 8;  //有效数据长度，新增一个供上位机传输额外控制命令：通知下位机是否强切指定PAGE
            Send_buf[3] = 0x00;    //
            Send_buf[4] = (byte)(index_tep & 0xFF);      //发送哪个绑点  0代表255绑点.
            Send_buf[5] = Convert.ToByte(datar >> 8);       //r
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(datag >> 8);      //g
            Send_buf[8] = Convert.ToByte(datag & 0xff);
            Send_buf[9] = Convert.ToByte(Datab >> 8);      //b
            Send_buf[10] = Convert.ToByte(Datab & 0xff);

            CRCData.CalculateCrc16(Send_buf, out Send_buf[11], out Send_buf[12]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 13);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }

        public void auto_gamma_write_RM692D9UDC_31(UInt16 datar, UInt16 datag, UInt16 Datab, int index_tep)//一组gamma写入
        {

            //获取当前的
            Send_buf = new byte[13];
            Setting.TempRecord.cmdLen = 13;
            Send_buf[0] = 0x68;//RM692D9
            Send_buf[1] = 0xfc;//AUTO Gamma 标志位
            Send_buf[2] = 8;  //有效数据长度，新增一个供上位机传输额外控制命令：通知下位机是否强切指定PAGE
            Send_buf[3] = 0x00;    //
            Send_buf[4] = (byte)(index_tep & 0xFF);      //发送哪个绑点  0代表255绑点.
            Send_buf[5] = Convert.ToByte(datar >> 8);       //r
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(datag >> 8);      //g
            Send_buf[8] = Convert.ToByte(datag & 0xff);
            Send_buf[9] = Convert.ToByte(Datab >> 8);      //b
            Send_buf[10] = Convert.ToByte(Datab & 0xff);

            CRCData.CalculateCrc16(Send_buf, out Send_buf[11], out Send_buf[12]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 13);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }

        private void T20_Tick(object sender, EventArgs e)
        {
            if (radioButtonbd15.Checked)
            {
                ILI7835_Gamma_Tuning(null, null);
            }
            else if (radioButtonbd31.Checked)
            {
                ILI7835_Gamma_Tuning31(null, null);
            }
            //double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            //double lum_A;
            //Msr_CA310_Vis(); //Thread.Sleep(500); Msr_CA310();
            //                 //获取当前亮度值并确定其是否在规格范围内   
            //lum_A = auto_lum;   //将调好的亮度变成目标亮度


            ////送一张白图
            //OLED_Show_Click(btnwhite, e);//pengcs
            //double j = 172;//AC 写一个j值送一个参考值
            //Write_GIR_Drop(Convert.ToUInt16(0xA9AA), Convert.ToUInt16(j));
            //Msr_CA310_Vis();

            //bool i = true;
            //while (i)
            //{
            //    Msr_CA310_Vis();
            //    if (System.Math.Abs(auto_lum - lum_A) <= 5)
            //    {
            //        Write_GIR_Drop(Convert.ToUInt16(0xA9AA), Convert.ToUInt16(j));
            //        Msr_CA310_Vis();
            //        T20.Enabled = false;
            //        i = false;
            //        R66451_btnPrevPic_Run("W255.bmp");//送一张黑白框图
            //        T19.Enabled = true;

            //    }
            //    else if (auto_lum < lum_A - 5)
            //    {
            //        j = j + 1;
            //        // Write_GIR_Drop(Convert.ToUInt16(0xADAF), Convert.ToUInt16(j)); //old
            //        Write_GIR_Drop(Convert.ToUInt16(0xA9AA), Convert.ToUInt16(j));  //new
            //        i = true;
            //        Msr_CA310_Vis();
            //    }
            //    else//auto_lum > lum_A +5
            //    {
            //        j = j - 1;

            //        Write_GIR_Drop(Convert.ToUInt16(0xA9AA), Convert.ToUInt16(j));  //new
            //        i = true;
            //        Msr_CA310_Vis();
            //    }
            //}

        }
        void Write_GIR_Drop(UInt16 Addr, UInt16 date)//一组gamma写入
        {
            Send_buf = new byte[9];
            Setting.TempRecord.cmdLen = 9;
            Send_buf[0] = 0x3F;
            Send_buf[1] = 0x0E;
            Send_buf[2] = 4;
            //old  //先传高位再传低位
            //Send_buf[3] = Convert.ToByte(Addr >> 8 & 0xff);//地址高位
            ////Send_buf[4] = Convert.ToByte((date >> 6 & 0x0c) + 0x01);//0x01为u2所用  //这句貌似写错 先注释
            //Send_buf[4] = Convert.ToByte((date >> 8 & 0x0c));//数据低位

            //Send_buf[5] = Convert.ToByte(Addr & 0xff);  //地址低位
            //Send_buf[6] = Convert.ToByte(date & 0xff);  //数据低位

            Send_buf[3] = Convert.ToByte(Addr >> 8 & 0xff);
            Send_buf[4] = Convert.ToByte((date >> 8 & 0xff));
            Send_buf[5] = Convert.ToByte(Addr & 0xff);
            //Send_buf[6] = Convert.ToByte(date & 0x0c);
            Send_buf[6] = Convert.ToByte(date & 0x8f);

            CRCData.CalculateCrc16(Send_buf, out Send_buf[7], out Send_buf[8]);
            try { MainForm.comPort.Write(Send_buf, 0, 9); }
            catch { MessageBox.Show("串口有误，请检查", "错误"); }
        }


        private void BtnComConnect_Click(object sender, EventArgs e)
        {
            MainForm.btnOpenCom_Click(null, e);
        }

        string cmdstr = "";
        private int[] Point_692AXTest = new int[15] {//---副屏临时调试用绑点数据---
            255, 240, 208, 144, 112, 80, 64, 40, 32, 24, 16, 8, 4, 1, 0 };
        private int[] Point_692AXDefault = new int[15] {//---Gamma默认绑点数据---
            255, 240, 208, 144, 112, 80, 64, 40, 32, 24, 16, 8, 4, 1, 0 };


        private void ChksubRadiobutton505256_CheckedChanged(object sender, EventArgs e)
        {
            if ((Page50.Checked) || (Page58.Checked))
            {
                button_qaingqie.Text = "强切";
            }
            else if (Page52H.Checked)
            {
                button_qaingqie.Text = "52切换";
            }
        }

        //15-31绑点切换
        private void ChksubRadiobutton1531_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonbd15.Checked)
            {
                RM692D9_0R16.Enabled = false;
                RM692D9_0R17.Enabled = false;
                RM692D9_0R18.Enabled = false;
                RM692D9_0R19.Enabled = false;
                RM692D9_0R20.Enabled = false;
                RM692D9_0R21.Enabled = false;
                RM692D9_0R22.Enabled = false;
                RM692D9_0R23.Enabled = false;
                RM692D9_0R24.Enabled = false;
                RM692D9_0R25.Enabled = false;
                RM692D9_0R26.Enabled = false;
                RM692D9_0R27.Enabled = false;
                RM692D9_0R28.Enabled = false;
                RM692D9_0R29.Enabled = false;
                RM692D9_0R30.Enabled = false;
                RM692D9_0R31.Enabled = false;
                RM692D9_0G16.Enabled = false;
                RM692D9_0G17.Enabled = false;
                RM692D9_0G18.Enabled = false;
                RM692D9_0G19.Enabled = false;
                RM692D9_0G20.Enabled = false;
                RM692D9_0G21.Enabled = false;
                RM692D9_0G22.Enabled = false;
                RM692D9_0G23.Enabled = false;
                RM692D9_0G24.Enabled = false;
                RM692D9_0G25.Enabled = false;
                RM692D9_0G26.Enabled = false;
                RM692D9_0G27.Enabled = false;
                RM692D9_0G28.Enabled = false;
                RM692D9_0G29.Enabled = false;
                RM692D9_0G30.Enabled = false;
                RM692D9_0G31.Enabled = false;
                RM692D9_0B16.Enabled = false;
                RM692D9_0B17.Enabled = false;
                RM692D9_0B18.Enabled = false;
                RM692D9_0B19.Enabled = false;
                RM692D9_0B20.Enabled = false;
                RM692D9_0B21.Enabled = false;
                RM692D9_0B22.Enabled = false;
                RM692D9_0B23.Enabled = false;
                RM692D9_0B24.Enabled = false;
                RM692D9_0B25.Enabled = false;
                RM692D9_0B26.Enabled = false;
                RM692D9_0B27.Enabled = false;
                RM692D9_0B28.Enabled = false;
                RM692D9_0B29.Enabled = false;
                RM692D9_0B30.Enabled = false;
                RM692D9_0B31.Enabled = false;
                //15
                this.listBox4.Items.Clear();
                this.listBox4.Items.AddRange(new object[] {
                "R0",
                "R1",
                "R3",
                "R7",
                "R15",
                "R23",
                "R31",
                "R47",
                "R63",
                "R79",
                "R111"});
                this.listBox6.Items.Clear();
                this.listBox6.Items.AddRange(new object[] {
                "G0",
                "G1",
                "G3",
                "G7",
                "G15",
                "G23",
                "G31",
                "G47",
                "G63",
                "G79",
                "G111"});
                this.listBox5.Items.Clear();
                this.listBox5.Items.AddRange(new object[] {
                "B0",
                "B1",
                "B3",
                "B7",
                "B15",
                "B23",
                "B31",
                "B47",
                "B63",
                "B79",
                "B111"});
                this.listBox1.Items.Clear();
                this.listBox1.Items.AddRange(new object[] {
                "R143",
                "R207",
                "R239",
                "R255"});
                this.listBox3.Items.Clear();
                this.listBox3.Items.AddRange(new object[] {
                "G143",
                "G207",
                "G239",
                "G255"});
                this.listBox2.Items.Clear();
                this.listBox2.Items.AddRange(new object[] {
                "B143",
                "B207",
                "B239",
                "B255"});
                ILI7835_Gamma_SetBox.Clear();
                ILI7835_Gamma_SetBox.Text =
                        "__GAMMA_SET(0) \r\n" +
                        "TUNING_SPEC(15, 450, 0.300, 0.315, 2.2) \r\n" +
                        "LXY_SPEC(255, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(239, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(207, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(143, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(111, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(79, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(63, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(47, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(31, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(23, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(15, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(7, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(3, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(1, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(0, 0.01, 0.01, 1)\r\n";

            }
            else if (radioButtonbd31.Checked)
            {
                RM692D9_0R16.Enabled = true;
                RM692D9_0R17.Enabled = true;
                RM692D9_0R18.Enabled = true;
                RM692D9_0R19.Enabled = true;
                RM692D9_0R20.Enabled = true;
                RM692D9_0R21.Enabled = true;
                RM692D9_0R22.Enabled = true;
                RM692D9_0R23.Enabled = true;
                RM692D9_0R24.Enabled = true;
                RM692D9_0R25.Enabled = true;
                RM692D9_0R26.Enabled = true;
                RM692D9_0R27.Enabled = true;
                RM692D9_0R28.Enabled = true;
                RM692D9_0R29.Enabled = true;
                RM692D9_0R30.Enabled = true;
                RM692D9_0R31.Enabled = true;
                RM692D9_0G16.Enabled = true;
                RM692D9_0G17.Enabled = true;
                RM692D9_0G18.Enabled = true;
                RM692D9_0G19.Enabled = true;
                RM692D9_0G20.Enabled = true;
                RM692D9_0G21.Enabled = true;
                RM692D9_0G22.Enabled = true;
                RM692D9_0G23.Enabled = true;
                RM692D9_0G24.Enabled = true;
                RM692D9_0G25.Enabled = true;
                RM692D9_0G26.Enabled = true;
                RM692D9_0G27.Enabled = true;
                RM692D9_0G28.Enabled = true;
                RM692D9_0G29.Enabled = true;
                RM692D9_0G30.Enabled = true;
                RM692D9_0G31.Enabled = true;
                RM692D9_0B16.Enabled = true;
                RM692D9_0B17.Enabled = true;
                RM692D9_0B18.Enabled = true;
                RM692D9_0B19.Enabled = true;
                RM692D9_0B20.Enabled = true;
                RM692D9_0B21.Enabled = true;
                RM692D9_0B22.Enabled = true;
                RM692D9_0B23.Enabled = true;
                RM692D9_0B24.Enabled = true;
                RM692D9_0B25.Enabled = true;
                RM692D9_0B26.Enabled = true;
                RM692D9_0B27.Enabled = true;
                RM692D9_0B28.Enabled = true;
                RM692D9_0B29.Enabled = true;
                RM692D9_0B30.Enabled = true;
                RM692D9_0B31.Enabled = true;
                //31
                this.listBox4.Items.Clear();
                this.listBox4.Items.AddRange(new object[] {
                "R0",
                "R3",
                "R4",
                "R8",
                "R12",
                "R16",
                "R20",
                "R28",
                "R36",
                "R44",
                "R52"});
                this.listBox6.Items.Clear();
                this.listBox6.Items.AddRange(new object[] {
                "G0",
                "G3",
                "G4",
                "G8",
                "G12",
                "G16",
                "G20",
                "G28",
                "G36",
                "G44",
                "G52"});
                this.listBox5.Items.Clear();
                this.listBox5.Items.AddRange(new object[] {
                "B0",
                "B3",
                "B4",
                "B8",
                "B12",
                "B16",
                "B20",
                "B28",
                "B36",
                "B44",
                "B52"});
                this.listBox1.Items.Clear();
                this.listBox1.Items.AddRange(new object[] {
                "R68",
                "R84",
                "R100",
                "R116"});
                this.listBox3.Items.Clear();
                this.listBox3.Items.AddRange(new object[] {
                "G68",
                "G84",
                "G100",
                "G116"});
                this.listBox2.Items.Clear();
                this.listBox2.Items.AddRange(new object[] {
                "B68",
                "B84",
                "B100",
                "B116"});

                ILI7835_Gamma_SetBox.Clear();
                ILI7835_Gamma_SetBox.Text =
                        "__GAMMA_SET(0) \r\n" +
                        "TUNING_SPEC(31, 450, 0.300, 0.315, 2.2) \r\n" +
                        "LXY_SPEC(255, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(254, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(252, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(248, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(244, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(236, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(228, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(220, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(212, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(204, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(196, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(188, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(180, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(164, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(148, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(132, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(116, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(100, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(84, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(68, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(52, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(44, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(36, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(28, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(20, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(16, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(12, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(8, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(4, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(3, 0.01, 0.01, 1)\r\n" +
                        "LXY_SPEC(0, 0.01, 0.01, 1)\r\n";

                //
            }
        }

        //


        private void RM692D9_Inter_Gamma1_Click(object sender, EventArgs e) //强切Page 
        {
            //FE,70
            //71,00
            //72,31
            //FE,50
            txtCmd4.Clear();
            txtCmd4.Text += "fe,70" + "\r\n";
            txtCmd4.Text += "71,00" + "\r\n";
            txtCmd4.Text += "72,31" + "\r\n";
            txtCmd4.Text += "fe,50" + "\r\n";
            Send_Cmd_Click(btnSend4, e);
        }

        //jisuan
        private void btn_ILI7835_Calculate_Click(object sender, EventArgs e)
        {
            double Lr_d, Lg_d, Lb_d;

            string[] arr = ILI7835_Gamma_SetBox.Lines;
            {
                try
                {
                    Diven_Gamma.Clear();
                    Diven_Offset.Clear();
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string line = arr[i];
                        line = line.Replace(" ", "");
                        if (line.Contains("TUNING_SPEC"))
                        {
                            line = line.Replace("TUNING_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr0 = line.Split(',');
                            Setting.Gamma_Property Gamma = new Setting.Gamma_Property(Convert.ToInt32(arr0[0]), Convert.ToDouble(arr0[1]), Convert.ToDouble(arr0[2]), Convert.ToDouble(arr0[3]), Convert.ToDouble(arr0[4]));
                            Diven_Gamma.Add(Gamma);
                        }
                        else if (line.Contains("LXY_SPEC"))
                        {
                            line = line.Replace("LXY_SPEC", "");
                            line = line.Replace("(", "");
                            line = line.Replace(")", "");
                            string[] arr1 = line.Split(',');
                            //将Offset对象放入集合
                            Setting.Offset_Property Offset = new Setting.Offset_Property(Convert.ToInt32(arr1[0]), Convert.ToDouble(arr1[1]), Convert.ToDouble(arr1[2]), Convert.ToDouble(arr1[3]));
                            Diven_Offset.Add(Offset);
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("格式输入有误");
                }
            }
            //----------------------------------------------------------------------------
            try
            {
                txt_ILI7835_loff.Text = Diven_Offset[0].LUM_OFF.ToString();
                txt_ILI7835_xoff.Text = Diven_Offset[0].X_OFF.ToString();
                txt_ILI7835_yoff.Text = Diven_Offset[0].Y_OFF.ToString();

                int Total_points = Diven_Gamma[0].Points_num;
                D_lums = new double[Total_points, 3];
                D_lum = new double[Total_points];
                txt_ILI7835_gamma.Text = Diven_Gamma[0].Curve.ToString();
                txt_ILI7835_lum.Text = Diven_Gamma[0].Lum.ToString();
                txt_ILI7835_x.Text = Diven_Gamma[0].X.ToString();
                txt_ILI7835_y.Text = Diven_Gamma[0].Y.ToString();
                Points_LV.Text = "";
                for (int p = 0; p < Total_points; p++)
                {
                    D_lum[p] = (Math.Pow((Diven_Offset[p].NO / 255.0), Diven_Gamma[0].Curve) * Diven_Gamma[0].Lum); //每绑点目标亮度
                    Points_LV.Text = Points_LV.Text + Diven_Offset[p].NO + ":  " + Math.Round(D_lum[p], 6) + "\r\n";
                }

                CaSDKObj.YUV_to_RGB(Diven_Gamma[0].Y, Diven_Gamma[0].X, out Lr_d, out Lb_d, out Lg_d);//R/B/G分亮度转换

                for (int i = 0; i < Total_points; i++)
                {
                    D_lums[i, 0] = D_lum[i] * Lr_d; //R/B/G分亮度计算
                    D_lums[i, 1] = D_lum[i] * Lg_d;
                    D_lums[i, 2] = D_lum[i] * Lb_d;
                }
            }
            catch
            {
                MessageBox.Show("格式输入有误");
            }
        }



        //OTP button
        private void button_OTP_Click(object sender, EventArgs e)
        {
            //获取当前的
            Send_buf = new byte[6];
            Setting.TempRecord.cmdLen = 6;
            Send_buf[3] = 0x00;
            if (checkBoxGAM1.Checked) { Send_buf[3] = (byte)(Send_buf[3] | 0x01); }
            if (checkBoxGAM5.Checked) { Send_buf[3] = (byte)(Send_buf[3] | 0x02); }
            if (checkBoxGAM9.Checked) { Send_buf[3] = (byte)(Send_buf[3] | 0x04); }
            if (checkBoxGAM20.Checked) { Send_buf[3] = (byte)(Send_buf[3] | 0x08); }
            if (checkBoxGAM21.Checked) { Send_buf[3] = (byte)(Send_buf[3] | 0x10); }
            Send_buf[0] = 0x68;//RM692D9
            Send_buf[1] = 0x07;//OTP 标志位
            Send_buf[2] = 1;  //有效数据长度，新增一个供上位机传输额外控制命令：通知下位机是否强切指定PAGE

            CRCData.CalculateCrc16(Send_buf, out Send_buf[4], out Send_buf[5]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 6);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }

        }

        //将各个Gam保存到指定ram中
        private void button_saveram_Click(object sender, EventArgs e)
        {
            //获取当前的
            Send_buf = new byte[6];
            Setting.TempRecord.cmdLen = 6;
            Send_buf[3] = 0x00;
            if (radioButtonGAM1.Checked) { Send_buf[3] = 0x01; }
            else if (radioButtonGAM5.Checked) { Send_buf[3] = 0x02; }
            else if (radioButtonGAM9.Checked) { Send_buf[3] = 0x03; }
            else if (radioButtonGAM20.Checked) { Send_buf[3] = 0x04; }
            else if (radioButtonGAM21.Checked) { Send_buf[3] = 0x05; }
            else
            {
                MessageBox.Show("请选择存入哪个RAM!!!");
                return;
            }
            Send_buf[0] = 0x68;//RM692D9
            Send_buf[1] = 0xDB;//写RAM标志 标志位
            Send_buf[2] = 1;  //有效数据长度，新增一个供上位机传输额外控制命令：通知下位机是否强切指定PAGE

            CRCData.CalculateCrc16(Send_buf, out Send_buf[4], out Send_buf[5]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 6);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }


        }

        //强切
        private void button_qaingqie_Click(object sender, EventArgs e)
        {
            //获取当前的
            Send_buf = new byte[6];
            Setting.TempRecord.cmdLen = 6;
            Send_buf[3] = 0x00;
            if (Page50.Checked) { Send_buf[3] = (byte)(Send_buf[3] | 0x01); }
            else if (Page58.Checked) { Send_buf[3] = (byte)(Send_buf[3] | 0x02); }
            else if (Page52H.Checked) { Send_buf[3] = (byte)(Send_buf[3] | 0x03); }
            Send_buf[0] = 0x68;//RM692D9
            Send_buf[1] = 0xAB;//强切 标志位
            Send_buf[2] = 1;  //有效数据长度，新增一个供上位机传输额外控制命令：通知下位机是否强切指定PAGE

            CRCData.CalculateCrc16(Send_buf, out Send_buf[4], out Send_buf[5]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 6);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }

        }


        //load infor
        private void RM692D9_692_Load(object sender, EventArgs e)
        {
            ChksubRadiobutton1531_CheckedChanged(null, null);
            //MainForm.Msr_CASDK();
        }

        //gen
        private void Gen_BC_Bar_Scroll(object sender, EventArgs e)
        {
            int bits_51h = Convert.ToInt16(Math.Pow(2, Convert.ToByte(Gen_51h_Bits.Text))) - 1;
            Gen_BC_Bar.Maximum = bits_51h;
            try
            {
                if (bits_51h < 0x100)
                {
                    Gen_BC_lab.Text = "51," + Gen_BC_Bar.Value.ToString("x2");
                }
                else
                {
                    if (checkBoxfan.Checked)   //IC里面的51值高低位相反了   TC1105 谱瑞IC是这样的
                    {
                        Gen_BC_lab.Text = "51" + "," + Gen_BC_Bar.Value.ToString("x4").Substring(2, 2) + "," + Gen_BC_Bar.Value.ToString("x4").Substring(0, 2);
                    }
                    else
                    {
                        Gen_BC_lab.Text = "51" + "," + Gen_BC_Bar.Value.ToString("x4").Substring(0, 2) + "," + Gen_BC_Bar.Value.ToString("x4").Substring(2, 2);
                    }

                }
                Generate_File_Click(Gen_DBC_control, null);
            }
            catch
            {
                MessageBox.Show("位深不支持！");
            }


        }

        //测量按钮
        private void button_calu_Click(object sender, EventArgs e)
        {
            MainForm.Msr_CASDK();
            auto_lum = MainForm.CASDK_LUM;
            auto_x = MainForm.CASDK_x;
            auto_y = MainForm.CASDK_y;

            //textBox_LV.Text = Convert.ToString(auto_lum);
            //textBox_X.Text = Convert.ToString(auto_x);
            //textBox_Y.Text = Convert.ToString(auto_y);
            textBox_LV.Text = auto_lum.ToString("#0.000000");
            textBox_X.Text = auto_x.ToString("#0.000000");
            textBox_Y.Text = auto_y.ToString("#0.000000");
        }

        private void RM692D9_692_FormClosed(object sender, FormClosedEventArgs e)
        {
            //返回主窗口
            MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);//移除本窗口串口处理函数
            MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);//
            MainForm.Show();
            this.Hide();
        }

        public void Generate_File_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            byte codes_count = 0;
            bool Transmit_info_flag = true;

            switch (btnTmp.Name)
            {
                case "Gen_DBC_control":
                    {
                        try
                        {
                            string strLine0 = Convert.ToString(Gen_BC_lab.Text);
                            string[] arr0 = strLine0.Split(','); //以逗号分割，添加到数组

                            Setting.TempRecord.cmdLen = arr0.Length + 5;
                            Send_buf = new byte[arr0.Length + 5];
                            Send_buf[0] = 0x3E; //
                            Send_buf[1] = 0x01; //寄存器写
                            Send_buf[2] = byte.Parse(arr0.Length.ToString());
                            for (int i = 0; i < arr0.Length; i++)
                            {
                                Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr0[i], 16)).ToString());
                            }
                            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
                            try
                            {
                                MainForm.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }
                        }
                        catch
                        {
                            MessageBox.Show("输入格式错误！", "错误");
                        }
                    }
                    break;
                default: break;
            }

            // T1.Enabled = false;
            //  T1.Interval = 5000;
            // T1.Enabled = true;
        }

        private void Gen_DBC_control_Click(object sender, EventArgs e)
        {

            Button btnTmp = (Button)sender;

            switch (btnTmp.Name)
            {


                case "Gen_DBC_control":
                    {
                        try
                        {
                            string strLine0 = Convert.ToString(Gen_BC_lab.Text);
                            string[] arr0 = strLine0.Split(','); //以逗号分割，添加到数组

                            Setting.TempRecord.cmdLen = arr0.Length + 5;
                            Send_buf = new byte[arr0.Length + 5];
                            Send_buf[0] = 0x3E; //
                            Send_buf[1] = 0x01; //寄存器写
                            Send_buf[2] = byte.Parse(arr0.Length.ToString());
                            for (int i = 0; i < arr0.Length; i++)
                            {
                                Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr0[i], 16)).ToString());
                            }
                            CRCData.CalculateCrc16(Send_buf, out Send_buf[Send_buf[2] + 3], out Send_buf[Send_buf[2] + 4]);
                            try
                            {
                                MainForm.comPort.Write(Send_buf, 0, Send_buf[2] + 5);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }
                        }
                        catch
                        {
                            MessageBox.Show("输入格式错误！", "错误");
                        }
                    }
                    break;
                default: break;
            }


        }

        private void T5_Tick(object sender, EventArgs e)
        {

        }
        int[] Point_692AXDefault_sll = new int[15] {//---Gamma默认绑点数据---
            255, 239, 207, 143, 111, 79, 63, 47, 31, 23, 15, 7, 3, 1, 0 };
        private void btn_ILI7835_AutoGammas_Click(object sender, EventArgs e)
        {
            index = 0;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            gamma_r = GAMMAs.ILI7835_Gamma_r;
            gamma_g = GAMMAs.ILI7835_Gamma_g;
            gamma_b = GAMMAs.ILI7835_Gamma_b;
            //------------flexible gamma use------------------
            MainForm.Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------
            temptest255level = false;

            Application.DoEvents();

            //sll--
            if (radioButtonbd15.Checked)
            {
                ;

            }



            if (!chkgirgamma1.Checked)
            {
                MainForm.OLED_Show_Click(MainForm.btnwhite, e);
            }

            numPatternR.Value = MainForm.numPatternR.Value = 255;
            numPatternG.Value = MainForm.numPatternG.Value = 255;
            numPatternB.Value = MainForm.numPatternB.Value = 255;
            if (!chkgirgamma1.Checked)
            {
                MainForm.OLED_Show_Click(MainForm.btnPatternW, e);
            }

            Thread.Sleep(10);

            if (!chkgirgamma1.Checked)
            {
                RM692D9_Gamma_Click(buttonW_Red, e);
                Thread.Sleep(10);
                RM692D9_Gamma_Click(buttonW_Green, e);
                Thread.Sleep(10);
                RM692D9_Gamma_Click(buttonW_Blue, e);
                Thread.Sleep(10);
            }

            btn_ILI7835_Calculate_Click(null, null);

            Thread.Sleep(10);
            if (Page58.Checked == true || checkBox3.Checked == true)
            {
                bool load_bmp_status = false;
                string bmppath = "";
                if (radioButtonbd15.Checked && Page58.Checked)
                {
                    bmppath = strcurrentpath + "/udcgammabmp/" + bmpname[index];
                }
                else if (radioButtonbd31.Checked && Page58.Checked)
                {
                    bmppath = strcurrentpath + "/udcgammabmp/31/" + bmpname31[index];
                }
                if (radioButtonbd15.Checked && checkBox3.Checked)
                {
                    bmppath = strcurrentpath + "\\aodgammabmp\\" + aodbmpname[index];
                }
                else if (radioButtonbd31.Checked && checkBox3.Checked)
                {
                    bmppath = strcurrentpath + "/aodgammabmp/31/" + bmpname31[index];
                }

                if (!File.Exists(bmppath))
                {
                    MessageBox.Show("图片不存在");
                    return;
                }

                load_bmp_status = loadbmpfile(bmppath);
                if (!load_bmp_status)
                {
                    MessageBox.Show("图片加载失败");
                    return;
                }
            }

            Thread.Sleep(10);
            T20.Enabled = true;
        }

        //stop
        private void btn_ILI7835_Stop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;

        }


        private void R66451_btnPrevPic_Run(string filenames)       //R66451
        {
            Int32 i = 0;
            Int32 HACT_True = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            Int32 H_Size = 1134;
            byte[] TemFileContent = new byte[1024 * 100];
            byte[] TemFileContent1 = new byte[1024 * 100];
            System.Int32 ByteReaded = 0x00;
            try
            {
                //string FileName = textBox2.Text + '\' + listBox3_BMPFile.SelectedItem.ToString();

                string Filename;
                Filename = System.Windows.Forms.Application.StartupPath + "\\Resources\\M2\\" + filenames;//需见文件夹

                FileStream aFile = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                ByteReaded = aFile.Read(TemFileContent, 0, 54);
                HACT_True = (Convert.ToInt32(TemFileContent[19]) << 8) + TemFileContent[18];
                VACT_True = Convert.ToInt32((TemFileContent[23] << 8) + TemFileContent[22]);
                j = 1;
                //if ((HACT_True == ((TemFileContent[19] << 8) + (TemFileContent[18]))) && (Convert.ToInt32(VACT.Value) == (TemFileContent[23] << 8) + (TemFileContent[22])))//BMP图片水平大小
                {
                    aFile.Seek(54, SeekOrigin.Begin);
                    while (j <= VACT_True)
                    {
                        if (HACT_True % 4 != 0)
                            aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                        else
                            aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                        ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 

                        try { MainForm.comPort.Write(TemFileContent, 0, ByteReaded); }
                        catch
                        { MessageBox.Show("串口有误，请检查", "错误"); break; }

                        m_txlen += ByteReaded;
                        //progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        //label14.Text = progressBar1.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        j++;
                    }

                    TemFileContent[0] = 0x5A;
                    try
                    {
                        MainForm.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                    }

                    aFile.Close();
                }

            }
            catch
            {
                MessageBox.Show("请选择文件夹", "错误");
            }
        }
        private void GIR_Stop_Drop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            T19.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
        }

        //LIR


        //Gamma调节一些点调不过，点此处跳过按钮,进下一个绑点测量
        //具体在此处做了小处理 if ((flag1 == 0 && flag2 == 0 && flag3 == 0) | (gammasetjumpflag))
        bool gammasetjumpflag = false;//jump 按钮
        private void gammasetjump_Click(object sender, EventArgs e)
        {
            gammasetjumpflag = true;
        }

        //暂停gamma调节，双击恢复可调



    }
}
